Expand description
Provides Merge trait that can be used to merge structs into single by it’s values:
trait Merge: Sized {
fn merge(&mut self, other: &mut Self);
}§Usage
The Merge trait can be used to merge two structs into single by values. The example
use case is merging configuration from different sources: environment variables,
multiple configuration files and command-line arguments, see the args.rs example.
Merge can be derived for structs. Also you can provide custom merge strategies
for any fields that don’t implement Merge trait.
A merge strategy is a function with the signature fn merge<T>(left: &mut T, right: &mut T)
that merges right into left. The submodules of this crate provide strategies for the
most common types, but you can also define your own strategies.
§Features
This crate has the following features:
derive(default): Enables the derive macro for theMergetrait using themerge_derivecrate.num: Enables the merge strategies in thenummodule that require thenum_traitscrate.std(default): Enables the merge strategies in thehashmapandvecmodules that require the standard library. If this feature is not set,merge2is ano_std.
§Example
use merge2::Merge;
#[derive(Merge)]
struct User {
// Fields with the skip attribute are skipped by Merge
#[merge(skip)]
pub name: &'static str,
pub location: Option<&'static str>,
// The strategy attribute is used to customize the merge behavior
#[merge(strategy = ::merge2::vec::append)]
pub groups: Vec<&'static str>,
}
let mut defaults = User {
name: "",
location: Some("Internet"),
groups: vec!["rust"],
};
let mut ferris = User {
name: "Ferris",
location: None,
groups: vec!["mascot"],
};
ferris.merge(&mut defaults);
assert_eq!("Ferris", ferris.name);
assert_eq!(Some("Internet"), ferris.location);
assert_eq!(vec!["mascot", "rust"], ferris.groups);Modules§
- any
- Merge strategies applicable to any types
- bool
- Merge strategies for
bool - hashmap
- Merge strategies for
HashMap - num
num - Merge strategies for numeric types
- option
- Merge strategies for
Option - ord
- Merge strategies for types that can be compared (
PartialOrd) - string
- Merge strategies for
String - vec
- Merge strategies for
Vec
Traits§
- Merge
- A trait for objects that can be merged.
Derive Macros§
- Merge
derive