[][src]Struct glsp_engine::Rest

pub struct Rest<'a, T>(_);

An adapter type which collects any number of trailing function arguments.

When binding a Rust function so that it can be called from GameLisp, if Rest<T> appears at the end of the function's parameter list, it will collect all of the function's trailing arguments, converting them into a temporary array of T which is usually stored on the stack.

Rest<T> can be dereferenced to a mutable slice, [T]. It's also iterable, yielding T, &T or &mut T as appropriate.

//a function which adds one or more integers together
fn add_integers(first: i32, rest: Rest<i32>) -> i32 {
	let mut accumulator = first;
	for integer in rest {
		accumulator += integer;
	}

	accumulator
}

//bind this function to a global variable
glsp::bind_rfn("add-integers", &add_integers)?;

//the function can now be called from GameLisp
eval!("
	(add-integers 42)
	(add-integers 10 20 30 40 50)
")?;

//or from Rust
Rest::with([20, 30, 40, 50].iter().copied(), |rest| {
	add_integers(10, rest);
});

It's possible to construct a custom Rest<T> yourself by calling Rest::with, but it's usually not elegant. Instead, consider defining a Rust function which receives a slice or a generic IntoIterator, and a wrapper which receives a Rest<T> and forwards it to the original function.

fn add_integers(first: i32, rest: &[i32]) -> i32 {
	rest.iter().fold(first, |a, b| a + *b)
}

glsp::bind_rfn("add_integers", &|first: i32, rest: Rest<i32>| -> i32 {
	add_integers(first, &*rest)
})?;

Implementations

impl<'a, T> Rest<'a, T>[src]

pub fn with<S, F, R>(src: S, f: F) -> R where
    S: IntoIterator<Item = T>,
    F: FnOnce(Rest<'_, T>) -> R, 
[src]

Trait Implementations

impl<'a, T> Deref for Rest<'a, T>[src]

type Target = [T]

The resulting type after dereferencing.

impl<'a, T> DerefMut for Rest<'a, T>[src]

impl<'a, T, I: SliceIndex<[T]>> Index<I> for Rest<'a, T>[src]

type Output = I::Output

The returned type after indexing.

impl<'a, T, I: SliceIndex<[T]>> IndexMut<I> for Rest<'a, T>[src]

impl<'a, T> IntoCallArgs for Rest<'a, T> where
    T: IntoVal
[src]

impl<'r, 'a: 'r, T> IntoCallArgs for &'r Rest<'a, T> where
    &'r T: IntoVal
[src]

impl<'r, 'a: 'r, T> IntoCallArgs for &'r mut Rest<'a, T> where
    &'r mut T: IntoVal
[src]

impl<'a, T> IntoIterator for Rest<'a, T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<[T; 8]>

Which kind of iterator are we turning this into?

impl<'r, 'a: 'r, T> IntoIterator for &'r Rest<'a, T>[src]

type Item = &'r T

The type of the elements being iterated over.

type IntoIter = Iter<'r, T>

Which kind of iterator are we turning this into?

impl<'r, 'a: 'r, T> IntoIterator for &'r mut Rest<'a, T>[src]

type Item = &'r mut T

The type of the elements being iterated over.

type IntoIter = IterMut<'r, T>

Which kind of iterator are we turning this into?

Auto Trait Implementations

impl<'a, T> RefUnwindSafe for Rest<'a, T> where
    T: RefUnwindSafe
[src]

impl<'a, T> Send for Rest<'a, T> where
    T: Send
[src]

impl<'a, T> Sync for Rest<'a, T> where
    T: Sync
[src]

impl<'a, T> Unpin for Rest<'a, T>[src]

impl<'a, T> !UnwindSafe for Rest<'a, T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Erased for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.