pub struct Iterable<F>(pub F);Expand description
Creates a JavaScript iterator from a Rust closure or iterator.
The returned JS object has next() and inherits
[Symbol.iterator]() { return this } from %IteratorPrototype%,
so it works with for...of, spread, and destructuring.
§From a closure
let mut i = 0;
let iter = Iterable::from_fn(move || {
i += 1;
if i <= 3 { Some(i) } else { None }
});
ctx.globals().set("myIter", iter)?;
let result: Vec<i32> = ctx.eval("[...myIter]")?;
assert_eq!(result, vec![1, 2, 3]);§From an iterator
let iter = Iterable::from(vec![1, 2, 3]);
ctx.globals().set("myIter", iter)?;
let result: Vec<i32> = ctx.eval("[...myIter]")?;
assert_eq!(result, vec![1, 2, 3]);Tuple Fields§
§0: FImplementations§
Trait Implementations§
Auto Trait Implementations§
impl<F> Freeze for Iterable<F>where
F: Freeze,
impl<F> RefUnwindSafe for Iterable<F>where
F: RefUnwindSafe,
impl<F> Send for Iterable<F>where
F: Send,
impl<F> Sync for Iterable<F>where
F: Sync,
impl<F> Unpin for Iterable<F>where
F: Unpin,
impl<F> UnsafeUnpin for Iterable<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for Iterable<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<'js, T> AsProperty<'js, T> for Twhere
T: IntoJs<'js>,
impl<'js, T> AsProperty<'js, T> for Twhere
T: IntoJs<'js>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more