pub trait Struct: Debug + PartialEq {
type FieldName: SymbolToken + ?Sized;
type Element: Element + ?Sized;
fn iter<'a>(
&'a self
) -> Box<dyn Iterator<Item = (&'a Self::FieldName, &'a Self::Element)> + 'a>;
fn get<T: AsRef<str>>(&self, field_name: T) -> Option<&Self::Element>;
fn get_all<'a, T: AsRef<str>>(
&'a self,
field_name: T
) -> Box<dyn Iterator<Item = &'a Self::Element> + 'a>;
}
Expand description
Represents the value of struct
of Ion elements.
Required Associated Types
Required Methods
The fields of the structure.
Note that this uses a Box<dyn Iterator<...>>
to capture the borrow cleanly without
without generic associated types (GAT). In theory, when GAT lands, this could
be replaced with static polymorphism.
Returns the last value corresponding to the field_name in the struct or
returns None
if the field_name does not exist in the struct
Usage
Using the borrowed API:
let fields: Vec<(&str, BorrowedValue)>= vec![("e", "f"), ("g", "h")]
.into_iter().map(|(k, v)| (k, BorrowedValue::String(v))).collect();
let borrowed: BorrowedStruct = fields.into_iter().collect();
assert_eq!("h", borrowed.get("g".to_string()).map(|e| e.as_str()).flatten().unwrap());
Using the owned API:
let fields: Vec<(&str, OwnedValue)>= vec![("a", "b"), ("c", "d")]
.into_iter().map(|(k, v)| (k, OwnedValue::String(v.into()))).collect();
let owned: OwnedStruct = fields.into_iter().collect();
assert_eq!("d", owned.get("c".to_string()).map(|e| e.as_str()).flatten().unwrap());
Returns an iterator with all the values corresponding to the field_name in the struct or returns an empty iterator if the field_name does not exist in the struct
Usage
Using the borrowed API:
let fields: Vec<(&str, BorrowedValue)>= vec![("a", "b"), ("c", "d"), ("c", "e")]
.into_iter().map(|(k, v)| (k, BorrowedValue::String(v))).collect();
let borrowed: BorrowedStruct = fields.into_iter().collect();
assert_eq!(
vec!["d", "e"],
borrowed.get_all("c").flat_map(|e| e.as_str()).collect::<Vec<&str>>()
);
Using the owned API:
let fields: Vec<(&str, OwnedValue)>= vec![("d", "e"), ("d", "f"), ("g", "h")]
.into_iter().map(|(k, v)| (k, OwnedValue::String(v.into()))).collect();
let owned: OwnedStruct = fields.into_iter().collect();
assert_eq!(
vec!["e", "f"],
owned.get_all("d").flat_map(|e| e.as_str()).collect::<Vec<&str>>()
);