summaryrefslogtreecommitdiff
path: root/serde_v8/src/serializable.rs
diff options
context:
space:
mode:
Diffstat (limited to 'serde_v8/src/serializable.rs')
-rw-r--r--serde_v8/src/serializable.rs22
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)
+ }
+}