pub trait Transform<'de> {
Show 20 methods
// Provided methods
fn push_path(&mut self, _key: &'de str) { ... }
fn pop_path(&mut self) { ... }
fn transform_bool(&mut self, v: bool) -> bool { ... }
fn transform_i8(&mut self, v: i8) -> i8 { ... }
fn transform_i16(&mut self, v: i16) -> i16 { ... }
fn transform_i32(&mut self, v: i32) -> i32 { ... }
fn transform_i64(&mut self, v: i64) -> i64 { ... }
fn transform_u8(&mut self, v: u8) -> u8 { ... }
fn transform_u16(&mut self, v: u16) -> u16 { ... }
fn transform_u32(&mut self, v: u32) -> u32 { ... }
fn transform_u64(&mut self, v: u64) -> u64 { ... }
fn transform_i128(&mut self, v: i128) -> i128 { ... }
fn transform_u128(&mut self, v: u128) -> u128 { ... }
fn transform_f32(&mut self, v: f32) -> f32 { ... }
fn transform_f64(&mut self, v: f64) -> f64 { ... }
fn transform_char(&mut self, v: char) -> char { ... }
fn transform_str<'a>(&mut self, v: &'a str) -> Cow<'a, str> { ... }
fn transform_string(&mut self, v: String) -> Cow<'static, str> { ... }
fn transform_bytes<'a>(&mut self, v: &'a [u8]) -> Cow<'a, [u8]> { ... }
fn transform_byte_buf(&mut self, v: Vec<u8>) -> Cow<'static, [u8]> { ... }
}
Expand description
A transform for deserialized values.
This transformer defines callbacks that will be called by a Deserializer
during
deserialization to map values inline. The default for every transform callback is the identity
function, which will not change the value.
There is a default implementation for all functions with a matching signature, for example
FnMut(&str) -> Cow<str>
.
§Strings and Bytes
When implementing a transform for strings or bytes, always implement both the owned and borrowed version:
transform_str
andtransform_string
for stringstransform_bytes
andtransform_byte_buf
for bytes.
§Numbers
If the deserializer is used on a format that supports all numeric types, the default of each
transform function is the identity. To override this, all of transform_i*
and transform_u*
have to be implemented.
§Example
ⓘ
struct StringDefault(&'static str);
impl Transform for StringDefault {
fn transform_str<'a>(&mut self, v: &'a str) -> Cow<'a, str> {
match v {
"" => Cow::Borrowed(self.0),
other => Cow::Borrowed(other),
}
}
fn transform_string(&mut self, v: String) -> Cow<'a, str> {
match v.as_str() {
"" => Cow::Borrowed(self.0),
_ => Cow::Owned(v),
}
}
}