1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/serde_v8/src/magic/string_or_buffer.rs
Aaron O'Mullan c27ef0ac7b
perf(http): encode string bodies in op-layer (#12451)
Using serde_v8's StringOrBuffer
2021-10-26 22:00:01 +02:00

44 lines
885 B
Rust

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 {
String(String),
Buffer(Vec<u8>),
}
impl StringOrBufferInner {
fn into_bytes(self) -> Vec<u8> {
match self {
Self::String(s) => s.into_bytes(),
Self::Buffer(b) => b,
}
}
}