summaryrefslogtreecommitdiff
path: root/serde_v8/magic/string_or_buffer.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2022-03-24 11:23:40 +0100
committerGitHub <noreply@github.com>2022-03-24 11:23:40 +0100
commit6516130b0138ef382a0588f983287fb463222086 (patch)
treef250150295fb4d7a023c1fa7d92da621ac804340 /serde_v8/magic/string_or_buffer.rs
parentc52d72e8e115f30be32cbb2a91a4c3aa117c328c (diff)
chore: drop src/ in bench_util & serde_v8 (#14097)
To align with conventions used in our other crates
Diffstat (limited to 'serde_v8/magic/string_or_buffer.rs')
-rw-r--r--serde_v8/magic/string_or_buffer.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/serde_v8/magic/string_or_buffer.rs b/serde_v8/magic/string_or_buffer.rs
new file mode 100644
index 000000000..edde2adcd
--- /dev/null
+++ b/serde_v8/magic/string_or_buffer.rs
@@ -0,0 +1,45 @@
+use std::ops::Deref;
+
+#[derive(Debug)]
+pub struct StringOrBuffer(Vec<u8>);
+
+impl Deref for StringOrBuffer {
+ type Target = Vec<u8>;
+ fn deref(&self) -> &Vec<u8> {
+ &self.0
+ }
+}
+
+impl StringOrBuffer {
+ pub fn into_bytes(self) -> Vec<u8> {
+ self.0
+ }
+}
+
+impl<'de> serde::Deserialize<'de> for StringOrBuffer {
+ fn deserialize<D>(deserializer: D) -> Result<StringOrBuffer, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ StringOrBufferInner::deserialize(deserializer)
+ .map(|x| StringOrBuffer(x.into_bytes()))
+ }
+}
+
+// TODO(@AaronO): explore if we can make this work with ZeroCopyBuf
+#[derive(serde::Deserialize)]
+#[serde(untagged)]
+enum StringOrBufferInner {
+ #[serde(with = "serde_bytes")]
+ Buffer(Vec<u8>),
+ String(String),
+}
+
+impl StringOrBufferInner {
+ fn into_bytes(self) -> Vec<u8> {
+ match self {
+ Self::String(s) => s.into_bytes(),
+ Self::Buffer(b) => b,
+ }
+ }
+}