| 1 | //! Path-based metadata to serialize with a value. |
| 2 | //! |
| 3 | //! Path-based in this context means that the metadata is linked |
| 4 | //! to the data in a relative and hierarchical fashion by tracking |
| 5 | //! the current absolute path of the field being serialized. |
| 6 | //! |
| 7 | //! # Example |
| 8 | //! |
| 9 | //! ``` |
| 10 | //! # use ron::ser::{PrettyConfig, path_meta::Field}; |
| 11 | //! |
| 12 | //! #[derive(serde::Serialize)] |
| 13 | //! struct Creature { |
| 14 | //! seconds_since_existing: usize, |
| 15 | //! linked: Option<Box<Self>>, |
| 16 | //! } |
| 17 | //! |
| 18 | //! let mut config = PrettyConfig::default(); |
| 19 | //! |
| 20 | //! config |
| 21 | //! .path_meta |
| 22 | //! // The path meta defaults to no root structure, |
| 23 | //! // so we either provide a prebuilt one or initialize |
| 24 | //! // an empty one to build. |
| 25 | //! .get_or_insert_with(Field::empty) |
| 26 | //! .build_fields(|fields| { |
| 27 | //! fields |
| 28 | //! // Get or insert the named field |
| 29 | //! .field("seconds_since_existing") |
| 30 | //! .with_doc("Outer seconds_since_existing"); |
| 31 | //! fields |
| 32 | //! .field("linked") |
| 33 | //! // Doc metadata is serialized preceded by three forward slashes and a space for each line |
| 34 | //! .with_doc("Optional.\nProvide another creature to be wrapped.") |
| 35 | //! // Even though it's another Creature, the fields have different paths, so they are addressed separately. |
| 36 | //! .build_fields(|fields| { |
| 37 | //! fields |
| 38 | //! .field("seconds_since_existing") |
| 39 | //! .with_doc("Inner seconds_since_existing"); |
| 40 | //! }); |
| 41 | //! }); |
| 42 | //! |
| 43 | //! let value = Creature { |
| 44 | //! seconds_since_existing: 0, |
| 45 | //! linked: Some(Box::new(Creature { |
| 46 | //! seconds_since_existing: 0, |
| 47 | //! linked: None, |
| 48 | //! })), |
| 49 | //! }; |
| 50 | //! |
| 51 | //! let s = ron::ser::to_string_pretty(&value, config).unwrap(); |
| 52 | //! |
| 53 | //! assert_eq!(s, r#"( |
| 54 | //! /// Outer seconds_since_existing |
| 55 | //! seconds_since_existing: 0, |
| 56 | //! /// Optional. |
| 57 | //! /// Provide another creature to be wrapped. |
| 58 | //! linked: Some(( |
| 59 | //! /// Inner seconds_since_existing |
| 60 | //! seconds_since_existing: 0, |
| 61 | //! linked: None, |
| 62 | //! )), |
| 63 | //! )"#); |
| 64 | //! ``` |
| 65 | //! |
| 66 | //! # Identical paths |
| 67 | //! |
| 68 | //! Especially in enums and tuples it's possible for fields |
| 69 | //! to share a path, thus being unable to be addressed separately. |
| 70 | //! |
| 71 | //! ```no_run |
| 72 | //! enum Kind { |
| 73 | //! A { |
| 74 | //! field: (), |
| 75 | //! }, // ^ |
| 76 | //! // cannot be addressed separately because they have the same path |
| 77 | //! B { // v |
| 78 | //! field: (), |
| 79 | //! }, |
| 80 | //! } |
| 81 | //! ``` |
| 82 | //! |
| 83 | //! ```no_run |
| 84 | //! struct A { |
| 85 | //! field: (), |
| 86 | //! } |
| 87 | //! |
| 88 | //! struct B { |
| 89 | //! field: (), |
| 90 | //! } |
| 91 | //! |
| 92 | //! type Value = ( |
| 93 | //! A, |
| 94 | //! // ^ |
| 95 | //! // These are different types, but they share the path `field` |
| 96 | //! // v |
| 97 | //! B, |
| 98 | //! ); |
| 99 | //! ``` |
| 100 | |
| 101 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#; |
| 102 | |
| 103 | use serde_deriverust-analyzer cargo serde_derive 1.0.228 crate/::{Deserializerust-analyzer cargo serde_derive 1.0.228 Deserialize:, Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:}; |
| 104 | |
| 105 | #[cfg(feature = "std")] |
| 106 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap# as FieldsInnerrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#; |
| 107 | |
| 108 | #[cfg(not(feature = "std"))] |
| 109 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::collectionsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/::BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap# as FieldsInnerrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#; |
| 110 | |
| 111 | /// The metadata and inner [`Fields`] of a field. |
| 112 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:, Deserializerust-analyzer cargo serde_derive 1.0.228 Deserialize:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 113 | pub struct Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field# { |
| 114 | docrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#doc.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 115 | fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#fields.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#>, |
| 116 | } |
| 117 | |
| 118 | impl Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field# { |
| 119 | /// Create a new empty field metadata. |
| 120 | #[must_use] |
| 121 | pub const fn emptyrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]empty().() -> Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field] { |
| 122 | Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field] { |
| 123 | docrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#doc.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(), |
| 124 | fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#fields.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// Create a new field metadata from parts. |
| 129 | pub fn newrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]new().(doc: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, fields: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#>) -> Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field] { |
| 130 | Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field] { |
| 131 | docrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#doc.: doc.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(), |
| 132 | fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#fields., |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// Get a shared reference to the documentation metadata of this field. |
| 137 | #[inline] |
| 138 | #[must_use] |
| 139 | pub fn docrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]doc().(&self) -> &str { |
| 140 | self.docrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#doc..as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() |
| 141 | } |
| 142 | |
| 143 | /// Get a mutable reference to the documentation metadata of this field. |
| 144 | #[inline] |
| 145 | #[must_use] |
| 146 | pub fn doc_mutrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]doc_mut().(&mut self) -> &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 147 | &mut self.docrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#doc. |
| 148 | } |
| 149 | |
| 150 | /// Set the documentation metadata of this field. |
| 151 | /// |
| 152 | /// ``` |
| 153 | /// # use ron::ser::path_meta::Field; |
| 154 | /// |
| 155 | /// let mut field = Field::empty(); |
| 156 | /// |
| 157 | /// assert_eq!(field.doc(), ""); |
| 158 | /// |
| 159 | /// field.with_doc("some meta"); |
| 160 | /// |
| 161 | /// assert_eq!(field.doc(), "some meta"); |
| 162 | /// ``` |
| 163 | pub fn with_docrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]with_doc().(&mut self, doc: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> &mut Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field] { |
| 164 | self.docrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#doc. = doc.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(); |
| 165 | self |
| 166 | } |
| 167 | |
| 168 | /// Get a shared reference to the inner fields of this field, if it has any. |
| 169 | #[must_use] |
| 170 | pub fn fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]fields().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#> { |
| 171 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#fields..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().() |
| 172 | } |
| 173 | |
| 174 | /// Get a mutable reference to the inner fields of this field, if it has any. |
| 175 | pub fn fields_mutrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]fields_mut().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&mut Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#> { |
| 176 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#fields..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().() |
| 177 | } |
| 178 | |
| 179 | /// Return whether this field has inner fields. |
| 180 | /// |
| 181 | /// ``` |
| 182 | /// # use ron::ser::path_meta::{Field, Fields}; |
| 183 | /// |
| 184 | /// let mut field = Field::empty(); |
| 185 | /// |
| 186 | /// assert!(!field.has_fields()); |
| 187 | /// |
| 188 | /// field.with_fields(Some(Fields::default())); |
| 189 | /// |
| 190 | /// assert!(field.has_fields()); |
| 191 | /// ``` |
| 192 | #[must_use] |
| 193 | pub fn has_fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]has_fields().(&self) -> bool { |
| 194 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#fields..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() |
| 195 | } |
| 196 | |
| 197 | /// Set the inner fields of this field. |
| 198 | /// |
| 199 | /// ``` |
| 200 | /// # use ron::ser::path_meta::{Field, Fields}; |
| 201 | /// |
| 202 | /// let mut field = Field::empty(); |
| 203 | /// |
| 204 | /// assert!(!field.has_fields()); |
| 205 | /// |
| 206 | /// field.with_fields(Some(Fields::default())); |
| 207 | /// |
| 208 | /// assert!(field.has_fields()); |
| 209 | /// |
| 210 | /// field.with_fields(None); |
| 211 | /// |
| 212 | /// assert!(!field.has_fields()); |
| 213 | /// ``` |
| 214 | pub fn with_fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]with_fields().(&mut self, fields: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#>) -> &mut Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field] { |
| 215 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#fields. = fields; |
| 216 | self |
| 217 | } |
| 218 | |
| 219 | /// Ergonomic shortcut for building some inner fields. |
| 220 | /// |
| 221 | /// ``` |
| 222 | /// # use ron::ser::path_meta::Field; |
| 223 | /// |
| 224 | /// let mut field = Field::empty(); |
| 225 | /// |
| 226 | /// field.build_fields(|fields| { |
| 227 | /// fields.field("inner field"); |
| 228 | /// }); |
| 229 | /// |
| 230 | /// assert_eq!(field.fields().map(|fields| fields.contains("inner field")), Some(true)); |
| 231 | /// ``` |
| 232 | pub fn build_fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]build_fields().(&mut self, builder: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&mut Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#)) -> &mut Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field] { |
| 233 | let mut fields = Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#::defaultrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields][Default]default().(); |
| 234 | builder(&mut fields); |
| 235 | self.with_fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]with_fields().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(fields)); |
| 236 | self |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /// Mapping of names to [`Field`]s. |
| 241 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:, Deserializerust-analyzer cargo serde_derive 1.0.228 Deserialize:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 242 | pub struct Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields# { |
| 243 | fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields.: FieldsInnerrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#>, |
| 244 | } |
| 245 | |
| 246 | impl Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields# { |
| 247 | /// Return a new, empty metadata field map. |
| 248 | #[must_use] |
| 249 | pub fn newrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]new().() -> Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields] { |
| 250 | Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]::defaultrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields][Default]default().() |
| 251 | } |
| 252 | |
| 253 | /// Return whether this field map contains no fields. |
| 254 | /// |
| 255 | /// ``` |
| 256 | /// # use ron::ser::path_meta::{Fields, Field}; |
| 257 | /// |
| 258 | /// let mut fields = Fields::default(); |
| 259 | /// |
| 260 | /// assert!(fields.is_empty()); |
| 261 | /// |
| 262 | /// fields.insert("", Field::empty()); |
| 263 | /// |
| 264 | /// assert!(!fields.is_empty()); |
| 265 | /// ``` |
| 266 | #[must_use] |
| 267 | pub fn is_emptyrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]is_empty().(&self) -> bool { |
| 268 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields..is_emptyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]is_empty().() |
| 269 | } |
| 270 | |
| 271 | /// Return whether this field map contains a field with the given name. |
| 272 | /// |
| 273 | /// ``` |
| 274 | /// # use ron::ser::path_meta::{Fields, Field}; |
| 275 | /// |
| 276 | /// let fields: Fields = [("a thing", Field::empty())].into_iter().collect(); |
| 277 | /// |
| 278 | /// assert!(fields.contains("a thing")); |
| 279 | /// assert!(!fields.contains("not a thing")); |
| 280 | /// ``` |
| 281 | pub fn containsrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]contains().(&self, name: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>) -> bool { |
| 282 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields..contains_keyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]contains_key().(name.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 283 | } |
| 284 | |
| 285 | /// Get a reference to the field with the provided `name`, if it exists. |
| 286 | /// |
| 287 | /// ``` |
| 288 | /// # use ron::ser::path_meta::{Fields, Field}; |
| 289 | /// |
| 290 | /// let fields: Fields = [("a thing", Field::empty())].into_iter().collect(); |
| 291 | /// |
| 292 | /// assert!(fields.get("a thing").is_some()); |
| 293 | /// assert!(fields.get("not a thing").is_none()); |
| 294 | /// ``` |
| 295 | pub fn getrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]get().(&self, name: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#> { |
| 296 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields..getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(name.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 297 | } |
| 298 | |
| 299 | /// Get a mutable reference to the field with the provided `name`, if it exists. |
| 300 | /// |
| 301 | /// ``` |
| 302 | /// # use ron::ser::path_meta::{Fields, Field}; |
| 303 | /// |
| 304 | /// let mut fields: Fields = [("a thing", Field::empty())].into_iter().collect(); |
| 305 | /// |
| 306 | /// assert!(fields.get_mut("a thing").is_some()); |
| 307 | /// assert!(fields.get_mut("not a thing").is_none()); |
| 308 | /// ``` |
| 309 | pub fn get_mutrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]get_mut().(&mut self, name: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&mut Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#> { |
| 310 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields..get_mutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get_mut().(name.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 311 | } |
| 312 | |
| 313 | /// Insert a field with the given name into the map. |
| 314 | /// |
| 315 | /// ``` |
| 316 | /// # use ron::ser::path_meta::{Fields, Field}; |
| 317 | /// |
| 318 | /// let mut fields = Fields::default(); |
| 319 | /// |
| 320 | /// assert!(fields.insert("field", Field::empty()).is_none()); |
| 321 | /// assert!(fields.insert("field", Field::empty()).is_some()); |
| 322 | /// ``` |
| 323 | pub fn insertrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]insert().(&mut self, name: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, field: Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#> { |
| 324 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(), field) |
| 325 | } |
| 326 | |
| 327 | /// Remove a field with the given name from the map. |
| 328 | /// |
| 329 | /// ``` |
| 330 | /// # use ron::ser::path_meta::{Fields, Field}; |
| 331 | /// |
| 332 | /// let mut fields: Fields = [("a", Field::empty())].into_iter().collect(); |
| 333 | /// |
| 334 | /// assert_eq!(fields.remove("a"), Some(Field::empty())); |
| 335 | /// assert_eq!(fields.remove("a"), None); |
| 336 | /// ``` |
| 337 | pub fn removerust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]remove().(&mut self, name: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#> { |
| 338 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields..removerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]remove().(name.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 339 | } |
| 340 | |
| 341 | /// Get a mutable reference to the field with the provided `name`, |
| 342 | /// inserting an empty [`Field`] if it didn't exist. |
| 343 | /// |
| 344 | /// ``` |
| 345 | /// # use ron::ser::path_meta::Fields; |
| 346 | /// |
| 347 | /// let mut fields = Fields::default(); |
| 348 | /// |
| 349 | /// assert!(!fields.contains("thing")); |
| 350 | /// |
| 351 | /// fields.field("thing"); |
| 352 | /// |
| 353 | /// assert!(fields.contains("thing")); |
| 354 | /// ``` |
| 355 | pub fn fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields]field().(&mut self, name: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> &mut Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field# { |
| 356 | self.fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields..entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]entry().(name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()).or_insert_withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`Entry<'a, K, V, A>`]or_insert_with().(Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#::emptyrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Field]empty().) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | impl<K: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#<(K, Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#)> for Fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields# { |
| 361 | fn from_iterrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields][`FromIterator<(K, Field)>`]from_iter().<T: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = (K, Fieldrust-analyzer cargo ron 0.12.0 ser/path_meta/Field#)>>(iter: T) -> Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields][`FromIterator<(K, Field)>`] { |
| 362 | Selfrust-analyzer cargo ron 0.12.0 ser/path_meta/impl#[Fields][`FromIterator<(K, Field)>`] { |
| 363 | fieldsrust-analyzer cargo ron 0.12.0 ser/path_meta/Fields#fields.: iter.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(k, v)| (k.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(), v)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 364 | } |
| 365 | } |
| 366 | } |