Skip to content

Commit

Permalink
feat(vm): Add serialization Pushable wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jun 19, 2017
1 parent 58983bd commit 712703e
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions vm/src/api/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ use value::{Def, Value};
use self::serde::ser::{self, Serialize};

pub fn to_value<T>(thread: &Thread, value: &T) -> Result<Value>
where T: Serialize
where T: ?Sized + Serialize
{
let mut context = thread.context();
let mut serializer = Serializer {
thread: thread,
context: &mut context,
};
value.serialize(&mut serializer)?;
Ok(serializer.context.stack.pop())
Ser(value).push(thread, &mut context)?;
Ok(context.stack.pop())
}

pub struct Ser<T>(pub T);

impl<'vm, T> Pushable<'vm> for Ser<T>
where T: Serialize
{
fn push(self, thread: &'vm Thread, context: &mut Context) -> Result<()> {
let mut serializer = Serializer {
thread: thread,
context: context,
};
self.0.serialize(&mut serializer)
}
}

impl ser::Error for Error {
Expand Down

0 comments on commit 712703e

Please sign in to comment.