Struct ion_rs::element::builders::StructBuilder
source · pub struct StructBuilder { /* private fields */ }
Expand description
Constructs Struct values incrementally.
use ion_rs::element::Element;
use ion_rs::ion_struct;
let actual: Element = ion_struct! {
"a": 1,
"b": true,
"c": "foo"
}
.into();
let expected = Element::read_one(r#"{a: 1, b: true, c: "foo"}"#).unwrap();
assert_eq!(actual, expected);
use ion_rs::element::{Element, Struct};
use ion_rs::ion_struct;
let base_struct: Struct = ion_struct! {
"foo": 1,
"bar": 2,
"baz": 3
};
let modified_struct: Element = base_struct
.clone_builder()
.remove_field("bar")
.with_field("quux", 4)
.build()
.into(); // Convert from `Struct` to `Element`
let expected = Element::read_one(r#"{foo: 1, baz: 3, quux: 4}"#).unwrap();
assert_eq!(expected, modified_struct);
Implementations§
source§impl StructBuilder
impl StructBuilder
sourcepub fn with_field<S: Into<Symbol>, E: Into<Element>>(
self,
field_name: S,
field_value: E
) -> Self
pub fn with_field<S: Into<Symbol>, E: Into<Element>>( self, field_name: S, field_value: E ) -> Self
Adds the provided (name, value)
pair to the Struct
being constructed.
sourcepub fn with_fields<S, E, I>(self, fields: I) -> Selfwhere
S: Into<Symbol>,
E: Into<Element>,
I: IntoIterator<Item = (S, E)>,
pub fn with_fields<S, E, I>(self, fields: I) -> Selfwhere S: Into<Symbol>, E: Into<Element>, I: IntoIterator<Item = (S, E)>,
Adds all of the provided (name, value)
pairs to the Struct
being constructed.
use ion_rs::element::{Element, Struct};
use ion_rs::ion_struct;
let struct1 = ion_struct! {
"foo": 1,
"bar": 2,
"baz": 3
};
let struct2 = ion_struct! {
"a": 4,
"b": 5,
"c": 6
};
let merged = struct1
.clone_builder()
.with_fields(struct2.fields())
.build();
let expected = Element::read_one("{foo: 1, bar: 2, baz: 3, a: 4, b: 5, c: 6}").unwrap();
sourcepub fn remove_field<A: AsRef<str>>(self, field_to_remove: A) -> Self
pub fn remove_field<A: AsRef<str>>(self, field_to_remove: A) -> Self
Removes the first field with the specified name from the Struct
being constructed.
Auto Trait Implementations§
impl RefUnwindSafe for StructBuilder
impl Send for StructBuilder
impl Sync for StructBuilder
impl Unpin for StructBuilder
impl UnwindSafe for StructBuilder
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