Skip to main content

Iterable

Struct Iterable 

Source
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: F

Implementations§

Source§

impl<F> Iterable<ClosureWrapper<F>>

Source

pub fn from_fn(f: F) -> Self

Create from a FnMut() -> Option<T> closure.

Trait Implementations§

Source§

impl<I: IntoIterator> From<I> for Iterable<IteratorWrapper<I::IntoIter>>

Source§

fn from(iter: I) -> Self

Converts to this type from the input type.
Source§

impl<'js, F> IntoJs<'js> for Iterable<F>
where F: IterableFn + 'js, F::Item: IntoJs<'js> + 'js,

Source§

fn into_js(self, ctx: &Ctx<'js>) -> Result<Value<'js>>

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<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'js, T> AsProperty<'js, T> for T
where T: IntoJs<'js>,

Source§

fn config( self, ctx: &Ctx<'js>, ) -> Result<(i32, Value<'js>, Value<'js>, Value<'js>), Error>

Property configuration Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<'js, T> IntoArg<'js> for T
where T: IntoJs<'js>,

Source§

fn num_args(&self) -> usize

The number of arguments this value produces.
Source§

fn into_arg(self, args: &mut Args<'js>) -> Result<(), Error>

Convert the value into an argument.
Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ParallelSend for T