pub trait Overwrite {
// Required method
fn overwrite<'de, D>(&mut self, d: D) -> Result<(), D::Error>
where D: Deserializer<'de>;
}Expand description
A trait for selectively overwriting fields in a struct from a deserializer.
The Overwrite trait provides a mechanism to update existing values in a struct
by deserializing only the fields present in the input data, leaving other fields
unchanged. This is particularly useful for configuration merging, partial updates,
and layered settings where you want to apply defaults first and then override
specific values.
§Behavior
- For primitive types and most standard library types,
overwritecompletely replaces the existing value with the deserialized value. - For structs that derive
Overwrite(using#[derive(Overwrite)]), only the fields present in the input are updated, while absent fields retain their current values.
§Examples
use serde::Deserialize;
use serde_extensions::Overwrite;
#[derive(Deserialize, Overwrite)]
struct Config {
host: String,
port: u16,
debug: bool,
}
let mut config = Config {
host: "localhost".to_string(),
port: 8080,
debug: false,
};
// Overwrite only the port, leaving host and debug unchanged
let partial = r#"{"port": 3000}"#;
config.overwrite(&mut serde_json::Deserializer::from_str(partial)).unwrap();
assert_eq!(config.host, "localhost");
assert_eq!(config.port, 3000);
assert_eq!(config.debug, false);§Implementing for Custom Types
For structs, you can derive Overwrite automatically:
use serde_extensions::Overwrite;
#[derive(Overwrite)]
struct MyStruct {
field1: String,
field2: i32,
}For other types, implement the trait manually. Types that should be completely replaced (rather than merged) should deserialize a new value and assign it:
use serde::{Deserialize, Deserializer};
use serde_extensions::Overwrite;
struct CustomType(i32);
impl Overwrite for CustomType {
fn overwrite<'de, D>(&mut self, d: D) -> Result<(), D::Error>
where
D: Deserializer<'de>,
{
*self = CustomType::deserialize(d)?;
Ok(())
}
}Required Methods§
Sourcefn overwrite<'de, D>(&mut self, d: D) -> Result<(), D::Error>where
D: Deserializer<'de>,
fn overwrite<'de, D>(&mut self, d: D) -> Result<(), D::Error>where
D: Deserializer<'de>,
Overwrites the current value with data from a deserializer.
For primitive types and collections, this completely replaces the value.
For structs with derived Overwrite, only fields present in the deserializer
are updated.
§Arguments
d- A deserializer containing the data to overwrite with
§Errors
Returns a deserialization error if the input data is invalid for the type.
§Examples
use serde_extensions::Overwrite;
let mut value = 42;
value.overwrite(&mut serde_json::Deserializer::from_str("100")).unwrap();
assert_eq!(value, 100);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.