Skip to content

Commit 36038d7

Browse files
committed
---
yaml --- r: 72189 b: refs/heads/dist-snap c: ae3b869 h: refs/heads/master i: 72187: 01d3df8 v: v3
1 parent 62e3d74 commit 36038d7

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
99
refs/heads/incoming: b50030718cf28f2a5a81857a26b57442734fe854
10-
refs/heads/dist-snap: c5baeb1db3d84e1ab0d14a8055db3a7d3cba638d
10+
refs/heads/dist-snap: ae3b8690c1e9c4debf20b4455ad50a79d5859ee9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1313
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/src/libcore/iterator.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub trait IteratorUtil<A> {
3939
fn take_while<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhileIterator<'r, A, Self>;
4040
fn skip(self, n: uint) -> SkipIterator<Self>;
4141
fn take(self, n: uint) -> TakeIterator<Self>;
42+
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
43+
-> ScanIterator<'r, A, B, Self, St>;
4244
fn advance(&mut self, f: &fn(A) -> bool);
4345
}
4446

@@ -93,6 +95,12 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
9395
TakeIterator{iter: self, n: n}
9496
}
9597

98+
#[inline(always)]
99+
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
100+
-> ScanIterator<'r, A, B, T, St> {
101+
ScanIterator{iter: self, f: f, state: initial_state}
102+
}
103+
96104
/// A shim implementing the `for` loop iteration protocol for iterator objects
97105
#[inline]
98106
fn advance(&mut self, f: &fn(A) -> bool) {
@@ -306,12 +314,13 @@ impl<A, T: Iterator<A>> Iterator<A> for TakeIterator<T> {
306314

307315
pub struct UnfoldrIterator<'self, A, St> {
308316
priv f: &'self fn(&mut St) -> Option<A>,
309-
priv state: St
317+
state: St
310318
}
311319

312320
pub impl<'self, A, St> UnfoldrIterator<'self, A, St> {
313321
#[inline]
314-
fn new(f: &'self fn(&mut St) -> Option<A>, initial_state: St) -> UnfoldrIterator<'self, A, St> {
322+
fn new(f: &'self fn(&mut St) -> Option<A>, initial_state: St)
323+
-> UnfoldrIterator<'self, A, St> {
315324
UnfoldrIterator {
316325
f: f,
317326
state: initial_state
@@ -326,6 +335,19 @@ impl<'self, A, St> Iterator<A> for UnfoldrIterator<'self, A, St> {
326335
}
327336
}
328337

338+
pub struct ScanIterator<'self, A, B, T, St> {
339+
priv iter: T,
340+
priv f: &'self fn(&mut St, A) -> Option<B>,
341+
state: St
342+
}
343+
344+
impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for ScanIterator<'self, A, B, T, St> {
345+
#[inline]
346+
fn next(&mut self) -> Option<B> {
347+
self.iter.next().chain(|a| (self.f)(&mut self.state, a))
348+
}
349+
}
350+
329351
#[cfg(test)]
330352
mod tests {
331353
use super::*;
@@ -406,6 +428,25 @@ mod tests {
406428
assert_eq!(i, ys.len());
407429
}
408430

431+
#[test]
432+
fn test_iterator_scan() {
433+
// test the type inference
434+
fn add(old: &mut int, new: &uint) -> Option<float> {
435+
*old += *new as int;
436+
Some(*old as float)
437+
}
438+
let xs = [0u, 1, 2, 3, 4];
439+
let ys = [0f, 1f, 3f, 6f, 10f];
440+
441+
let mut it = xs.iter().scan(0, add);
442+
let mut i = 0;
443+
for it.advance |x| {
444+
assert_eq!(x, ys[i]);
445+
i += 1;
446+
}
447+
assert_eq!(i, ys.len());
448+
}
449+
409450
#[test]
410451
fn test_unfoldr() {
411452
fn count(st: &mut uint) -> Option<uint> {

branches/dist-snap/src/libcore/sys.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,24 @@ pub fn get_type_desc<T>() -> *TypeDesc {
8383
unsafe { rusti::get_tydesc::<T>() as *TypeDesc }
8484
}
8585

86+
/// Returns a pointer to a type descriptor.
87+
#[inline(always)]
88+
pub fn get_type_desc_val<T>(_val: &T) -> *TypeDesc {
89+
get_type_desc::<T>()
90+
}
91+
8692
/// Returns the size of a type
8793
#[inline(always)]
8894
pub fn size_of<T>() -> uint {
8995
unsafe { rusti::size_of::<T>() }
9096
}
9197

98+
/// Returns the size of the type that `_val` points to
99+
#[inline(always)]
100+
pub fn size_of_val<T>(_val: &T) -> uint {
101+
size_of::<T>()
102+
}
103+
92104
/**
93105
* Returns the size of a type, or 1 if the actual size is zero.
94106
*
@@ -100,6 +112,13 @@ pub fn nonzero_size_of<T>() -> uint {
100112
if s == 0 { 1 } else { s }
101113
}
102114

115+
/// Returns the size of the type of the value that `_val` points to
116+
#[inline(always)]
117+
pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
118+
nonzero_size_of::<T>()
119+
}
120+
121+
103122
/**
104123
* Returns the ABI-required minimum alignment of a type
105124
*
@@ -111,12 +130,26 @@ pub fn min_align_of<T>() -> uint {
111130
unsafe { rusti::min_align_of::<T>() }
112131
}
113132

133+
/// Returns the ABI-required minimum alignment of the type of the value that
134+
/// `_val` points to
135+
#[inline(always)]
136+
pub fn min_align_of_val<T>(_val: &T) -> uint {
137+
min_align_of::<T>()
138+
}
139+
114140
/// Returns the preferred alignment of a type
115141
#[inline(always)]
116142
pub fn pref_align_of<T>() -> uint {
117143
unsafe { rusti::pref_align_of::<T>() }
118144
}
119145

146+
/// Returns the preferred alignment of the type of the value that
147+
/// `_val` points to
148+
#[inline(always)]
149+
pub fn pref_align_of_val<T>(_val: &T) -> uint {
150+
pref_align_of::<T>()
151+
}
152+
120153
/// Returns the refcount of a shared box (as just before calling this)
121154
#[inline(always)]
122155
pub fn refcount<T>(t: @T) -> uint {
@@ -162,7 +195,7 @@ pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! {
162195
#[cfg(test)]
163196
mod tests {
164197
use cast;
165-
use sys::{Closure, pref_align_of, size_of, nonzero_size_of};
198+
use sys::*;
166199
167200
#[test]
168201
fn size_of_basic() {
@@ -188,6 +221,14 @@ mod tests {
188221
assert!(size_of::<*uint>() == 8u);
189222
}
190223

224+
#[test]
225+
fn size_of_val_basic() {
226+
assert_eq!(size_of_val(&1u8), 1);
227+
assert_eq!(size_of_val(&1u16), 2);
228+
assert_eq!(size_of_val(&1u32), 4);
229+
assert_eq!(size_of_val(&1u64), 8);
230+
}
231+
191232
#[test]
192233
fn nonzero_size_of_basic() {
193234
type Z = [i8, ..0];
@@ -196,6 +237,14 @@ mod tests {
196237
assert!(nonzero_size_of::<uint>() == size_of::<uint>());
197238
}
198239

240+
#[test]
241+
fn nonzero_size_of_val_basic() {
242+
let z = [0u8, ..0];
243+
assert_eq!(size_of_val(&z), 0u);
244+
assert_eq!(nonzero_size_of_val(&z), 1u);
245+
assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
246+
}
247+
199248
#[test]
200249
fn align_of_basic() {
201250
assert!(pref_align_of::<u8>() == 1u);
@@ -219,6 +268,13 @@ mod tests {
219268
assert!(pref_align_of::<*uint>() == 8u);
220269
}
221270

271+
#[test]
272+
fn align_of_val_basic() {
273+
assert_eq!(pref_align_of_val(&1u8), 1u);
274+
assert_eq!(pref_align_of_val(&1u16), 2u);
275+
assert_eq!(pref_align_of_val(&1u32), 4u);
276+
}
277+
222278
#[test]
223279
fn synthesize_closure() {
224280
unsafe {

0 commit comments

Comments
 (0)