pub struct SortAlphabetically<T: Serialize>(pub T);Expand description
This is used to serialize any value implementing Serialize alphabetically.
The reason for this is to maintain consistency in the generated JSON string,
which is useful for diffing. The default serializer keeps the order of the
fields as they are defined in the struct, which will not be consistent when
there are extra fields.
§Example
use std::collections::BTreeMap;
use serde::Serialize;
use ruff_notebook::SortAlphabetically;
#[derive(Serialize)]
struct MyStruct {
a: String,
#[serde(flatten)]
extra: BTreeMap<String, String>,
b: String,
}
let my_struct = MyStruct {
a: "a".to_string(),
extra: BTreeMap::from([
("d".to_string(), "d".to_string()),
("c".to_string(), "c".to_string()),
]),
b: "b".to_string(),
};
let serialized = serde_json::to_string_pretty(&SortAlphabetically(&my_struct)).unwrap();
assert_eq!(
serialized,
r#"{
"a": "a",
"b": "b",
"c": "c",
"d": "d"
}"#
);Tuple Fields§
§0: TTrait Implementations§
Auto Trait Implementations§
impl<T> Freeze for SortAlphabetically<T>where
T: Freeze,
impl<T> RefUnwindSafe for SortAlphabetically<T>where
T: RefUnwindSafe,
impl<T> Send for SortAlphabetically<T>where
T: Send,
impl<T> Sync for SortAlphabetically<T>where
T: Sync,
impl<T> Unpin for SortAlphabetically<T>where
T: Unpin,
impl<T> UnsafeUnpin for SortAlphabetically<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for SortAlphabetically<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more