From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- third_party/rust/ron/examples/encode.rs | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 third_party/rust/ron/examples/encode.rs (limited to 'third_party/rust/ron/examples/encode.rs') diff --git a/third_party/rust/ron/examples/encode.rs b/third_party/rust/ron/examples/encode.rs new file mode 100644 index 0000000000..ad1a237ba5 --- /dev/null +++ b/third_party/rust/ron/examples/encode.rs @@ -0,0 +1,49 @@ +use ron::ser::{to_string_pretty, PrettyConfig}; +use serde::Serialize; +use std::{collections::HashMap, iter::FromIterator}; + +#[derive(Serialize)] +struct Config { + float: (f32, f64), + tuple: TupleStruct, + map: HashMap, + nested: Nested, + var: Variant, + array: Vec<()>, +} + +#[derive(Serialize)] +struct TupleStruct((), bool); + +#[derive(Serialize)] +enum Variant { + A(u8, &'static str), +} + +#[derive(Serialize)] +struct Nested { + a: String, + b: char, +} + +fn main() { + let data = Config { + float: (2.18, -1.1), + tuple: TupleStruct((), false), + map: HashMap::from_iter(vec![(0, '1'), (1, '2'), (3, '5'), (8, '1')]), + nested: Nested { + a: "Hello from \"RON\"".to_string(), + b: 'b', + }, + var: Variant::A(!0, ""), + array: vec![(); 3], + }; + + let pretty = PrettyConfig::new() + .depth_limit(2) + .separate_tuple_members(true) + .enumerate_arrays(true); + let s = to_string_pretty(&data, pretty).expect("Serialization failed"); + + println!("{}", s); +} -- cgit v1.2.3