use std::ops::Deref; #[derive(Debug)] pub struct StringOrBuffer(Vec); impl Deref for StringOrBuffer { type Target = Vec; fn deref(&self) -> &Vec { &self.0 } } impl StringOrBuffer { pub fn into_bytes(self) -> Vec { self.0 } } impl<'de> serde::Deserialize<'de> for StringOrBuffer { fn deserialize(deserializer: D) -> Result 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), String(String), } impl StringOrBufferInner { fn into_bytes(self) -> Vec { match self { Self::String(s) => s.into_bytes(), Self::Buffer(b) => b, } } }