[][src]Struct gluon_vm::api::ser::Ser

pub struct Ser<T>(pub T);

Pushable wrapper which pushes T by serializing it.

Struct

#[macro_use]
extern crate serde_derive;

extern crate gluon;
#[macro_use]
extern crate gluon_vm;

use gluon::{Thread, ThreadExt, new_vm};
use gluon::base::types::ArcType;
use gluon::vm::api::{FunctionRef, VmType};
use gluon::vm::api::ser::Ser;

#[derive(Serialize)]
struct Vec2 {
    x: i32,
    y: i32,
}

impl VmType for Vec2 {
    type Type = Self;

    fn make_type(thread: &Thread) -> ArcType {
        field_decl!{ x, y }
        type T = record_type! {
            x => i32,
            y => i32
        };
        T::make_type(thread)
    }
}


let thread = new_vm();

let (mut f, _): (FunctionRef<fn (Ser<Vec2>) -> i32>, _) = thread
    .run_expr("", r#"let f v: _ -> Int = v.x + v.y in f"#)
    .unwrap_or_else(|err| panic!("{}", err));
let vec = Vec2 {
    x: 3,
    y: 10
};
let result = f.call(Ser(vec)).unwrap_or_else(|err| panic!("{}", err));
assert_eq!(result, 13);

Enum

#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate gluon_vm;

use gluon::{Thread, ThreadExt, new_vm};
use gluon::base::types::ArcType;
use gluon::vm::api::{FunctionRef, VmType};
use gluon::vm::api::ser::Ser;


#[derive(Serialize)]
enum Enum {
    A(i32),
    B(String, i32),
    C { foo: f64, bar: i32 },
}

impl VmType for Enum {
    type Type = Self;

    fn make_type(thread: &Thread) -> ArcType {
        // Use the enum type declared in gluon
        thread.find_type_info("test.Enum").unwrap().into_type()
    }
}


let thread = new_vm();

let expr = r#"
type Enum = | A Int | B String Int | C { foo : Float, bar : Int }

let extract_bar r : { bar : Int | r } -> Int = r.bar

let f e =
    match e with
    | A a -> a
    | B b c -> c
    | C c -> extract_bar c

{ Enum, f }
"#;
thread
    .load_script("test", expr)
    .unwrap_or_else(|err| panic!("{}", err));

let mut f: FunctionRef<fn (Ser<Enum>) -> i32> = thread
    .get_global("test.f")
    .unwrap_or_else(|err| panic!("{}", err));

let result1 = f.call(Ser(Enum::B("".to_string(), 4)))
    .unwrap_or_else(|err| panic!("{}", err));
assert_eq!(result1, 4);

let result2 = f.call(Ser(Enum::C { foo: 3.14, bar: 10 }))
    .unwrap_or_else(|err| panic!("{}", err));
assert_eq!(result2, 10);

Trait Implementations

impl<'vm, T> Pushable<'vm> for Ser<T> where
    T: Serialize
[src]

impl<T> VmType for Ser<T> where
    T: VmType
[src]

type Type = T::Type

A version of Self which implements Any allowing a TypeId to be retrieved

Auto Trait Implementations

impl<T> RefUnwindSafe for Ser<T> where
    T: RefUnwindSafe

impl<T> Send for Ser<T> where
    T: Send

impl<T> Sync for Ser<T> where
    T: Sync

impl<T> Unpin for Ser<T> where
    T: Unpin

impl<T> UnwindSafe for Ser<T> where
    T: UnwindSafe

Blanket Implementations

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

impl<T> Any for T where
    T: Any

impl<'vm, T> AsyncPushable<'vm> for T where
    T: Pushable<'vm>, 
[src]

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

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

impl<'_, T> Captures<'_> for T[src]

impl<Choices> CoproductSubsetter<CNil, HNil> for Choices[src]

type Remainder = Choices

impl<T> Downcast for T where
    T: Any
[src]

impl<T> DowncastArc for T where
    T: Downcast + Send + Sync
[src]

impl<T> DowncastSync for T where
    T: Send + Sync + Any
[src]

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

impl<D, T> FromPtr<D> for T[src]

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

impl<T, U, I> LiftInto<U, I> for T where
    U: LiftFrom<T, I>, 
[src]

impl<'vm, T> Pushable<'vm> for T where
    T: Userdata
[src]

impl<Source> Sculptor<HNil, HNil> for Source[src]

type Remainder = Source

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.