diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-04 01:17:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-04 01:17:02 +0200 |
commit | 19d0e6b6710a61e6fdcf9f08e6057e49b349fe18 (patch) | |
tree | 8f444a1e26de1f483972f489e38d67ffc4775b3b /serde_v8/src/serializable.rs | |
parent | 878599ca7c4eb7636b6d025e669b39651f5ba1d0 (diff) |
perf(serde_v8): introduce Serializable boxable object (#9983)
Diffstat (limited to 'serde_v8/src/serializable.rs')
-rw-r--r-- | serde_v8/src/serializable.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/serde_v8/src/serializable.rs b/serde_v8/src/serializable.rs new file mode 100644 index 000000000..2e781340b --- /dev/null +++ b/serde_v8/src/serializable.rs @@ -0,0 +1,22 @@ +use rusty_v8 as v8; + +/// Serializable exists to allow boxing values as "objects" to be serialized later, +/// this is particularly useful for async op-responses. This trait is a more efficient +/// replacement for erased-serde that makes less allocations, since it's specific to serde_v8 +/// (and thus doesn't have to have generic outputs, etc...) +pub trait Serializable { + fn to_v8<'a>( + &self, + scope: &mut v8::HandleScope<'a>, + ) -> Result<v8::Local<'a, v8::Value>, crate::Error>; +} + +/// Allows all implementors of `serde::Serialize` to implement Serializable +impl<T: serde::Serialize> Serializable for T { + fn to_v8<'a>( + &self, + scope: &mut v8::HandleScope<'a>, + ) -> Result<v8::Local<'a, v8::Value>, crate::Error> { + crate::to_v8(scope, self) + } +} |