[][src]Trait glsp_engine::FromVal

pub trait FromVal: Sized + StaticMarker {
    pub fn from_val(val: &Val) -> GResult<Self>;
}

A type which can be converted from a GameLisp value.

Many functions in the glsp crate have a generic return value R: FromVal. They can automatically convert their return value to many different Rust types.

let numbers: Vec<u8> = glsp::global("numbers")?;
let text: Root<Str> = arr.pop()?;

Writing T::from_val(v)? is usually the most convenient way to destructure a Val. FromVal is part of the prelude, so there's usually no need to import it into scope.

let f = f64::from_val(val)?;

We provide FromVal implementations for many common types, including all of Rust's primitive integer and floating-point types; primitive Rust types like bool; most standard collections, including arrays, slices and tuples; Root and RRoot; type-erased enums like Deque and Callable; and owned string types, including PathBuf, OsString and CString.

You can also implement FromVal for your own types, which will enable them to take advantage of automatic conversions when they're bound as an RFn parameter.

Implementing FromVal for your own types currently requires the min_specialization nightly feature. Enable it by writing #![feature(min_specialization)] at the top of your crate's main.rs or lib.rs file.

struct Rgb(u8, u8, u8);

impl FromVal for Rgb {
	fn from_val(val: &Val) -> GResult<Rgb> {
		let (r, g, b) = <(u8, u8, u8)>::from_val(val)?; 
		Ok(Rgb(r, g, b))
	}
}

fn describe_rgb(rgb: Rgb) {
	let Rgb(r, g, b) = rgb;
	println!("Red: {}\nGreen: {}\nBlue: {}", r, g, b);
}

glsp::bind_rfn("describe-rgb", &describe_rgb)?;

//calling (describe-rgb '(32 178 178)) from a GameLisp script will
//automatically invoke Rgb::from_val, converting the array of three
//integers into an Rgb struct

Required methods

pub fn from_val(val: &Val) -> GResult<Self>[src]

Loading content...

Implementations on Foreign Types

impl FromVal for i32[src]

impl FromVal for i64[src]

impl FromVal for i128[src]

impl FromVal for isize[src]

impl FromVal for char[src]

impl FromVal for bool[src]

impl FromVal for i8[src]

impl FromVal for i16[src]

impl FromVal for u8[src]

impl FromVal for u16[src]

impl FromVal for u32[src]

impl FromVal for u64[src]

impl FromVal for u128[src]

impl FromVal for usize[src]

impl FromVal for f32[src]

impl FromVal for f64[src]

impl FromVal for Ordering[src]

impl<T: FromVal> FromVal for Vec<T>[src]

impl<T: FromVal> FromVal for VecDeque<T>[src]

impl<A> FromVal for SmallVec<A> where
    A: Array + StaticMarker,
    A::Item: FromVal
[src]

impl<T: FromVal, const N: usize> FromVal for [T; N][src]

impl<A> FromVal for (A,) where
    A: FromVal
[src]

impl<A, B> FromVal for (A, B) where
    A: FromVal,
    B: FromVal
[src]

impl<A, B, C> FromVal for (A, B, C) where
    A: FromVal,
    B: FromVal,
    C: FromVal
[src]

impl<A, B, C, D> FromVal for (A, B, C, D) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal
[src]

impl<A, B, C, D, E> FromVal for (A, B, C, D, E) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal,
    E: FromVal
[src]

impl<A, B, C, D, E, F> FromVal for (A, B, C, D, E, F) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal,
    E: FromVal,
    F: FromVal
[src]

impl<A, B, C, D, E, F, G> FromVal for (A, B, C, D, E, F, G) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal,
    E: FromVal,
    F: FromVal,
    G: FromVal
[src]

impl<A, B, C, D, E, F, G, H> FromVal for (A, B, C, D, E, F, G, H) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal,
    E: FromVal,
    F: FromVal,
    G: FromVal,
    H: FromVal
[src]

impl<A, B, C, D, E, F, G, H, I> FromVal for (A, B, C, D, E, F, G, H, I) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal,
    E: FromVal,
    F: FromVal,
    G: FromVal,
    H: FromVal,
    I: FromVal
[src]

impl<A, B, C, D, E, F, G, H, I, J> FromVal for (A, B, C, D, E, F, G, H, I, J) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal,
    E: FromVal,
    F: FromVal,
    G: FromVal,
    H: FromVal,
    I: FromVal,
    J: FromVal
[src]

impl<A, B, C, D, E, F, G, H, I, J, K> FromVal for (A, B, C, D, E, F, G, H, I, J, K) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal,
    E: FromVal,
    F: FromVal,
    G: FromVal,
    H: FromVal,
    I: FromVal,
    J: FromVal,
    K: FromVal
[src]

impl<A, B, C, D, E, F, G, H, I, J, K, L> FromVal for (A, B, C, D, E, F, G, H, I, J, K, L) where
    A: FromVal,
    B: FromVal,
    C: FromVal,
    D: FromVal,
    E: FromVal,
    F: FromVal,
    G: FromVal,
    H: FromVal,
    I: FromVal,
    J: FromVal,
    K: FromVal,
    L: FromVal
[src]

impl FromVal for String[src]

impl FromVal for CString[src]

impl FromVal for PathBuf[src]

impl FromVal for OsString[src]

impl<K, V, S> FromVal for HashMap<K, V, S> where
    K: HashEqMarker + FromVal + StaticMarker,
    V: FromVal + StaticMarker,
    S: BuildHasherDefaultMarker + StaticMarker, 
[src]

impl<K, V> FromVal for BTreeMap<K, V> where
    K: OrdMarker + FromVal + StaticMarker,
    V: FromVal + StaticMarker, 
[src]

Loading content...

Implementors

impl FromVal for Callable[src]

impl FromVal for Deque[src]

impl FromVal for EnvMode[src]

impl FromVal for Expander[src]

impl FromVal for Iterable[src]

impl FromVal for Num[src]

impl FromVal for Val[src]

impl FromVal for Root<Arr>[src]

impl FromVal for Root<Class>[src]

impl FromVal for Root<Coro>[src]

impl FromVal for Root<GFn>[src]

impl FromVal for Root<GIter>[src]

impl FromVal for Root<Obj>[src]

impl FromVal for Root<RData>[src]

impl FromVal for Root<RFn>[src]

impl FromVal for Root<Str>[src]

impl FromVal for Root<Tab>[src]

impl FromVal for Sym[src]

impl<T: StaticMarker> FromVal for RRoot<T>[src]

Loading content...