| 1 | //! Roundtrip serde Options module. |
| 2 | |
| 3 | 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#; |
| 4 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/; |
| 5 | |
| 6 | use serderust-analyzer cargo serde 1.0.228 crate/::{derust-analyzer cargo serde_core 1.0.228 de/, serrust-analyzer cargo serde_core 1.0.228 ser/}; |
| 7 | 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:}; |
| 8 | |
| 9 | use craterust-analyzer cargo ron 0.12.0 crate/::{ |
| 10 | derust-analyzer cargo ron 0.12.0 de/::Deserializerrust-analyzer cargo ron 0.12.0 de/Deserializer#, |
| 11 | errorrust-analyzer cargo ron 0.12.0 error/::{Resultrust-analyzer cargo ron 0.12.0 error/Result#, SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#}, |
| 12 | extensionsrust-analyzer cargo ron 0.12.0 extensions/::Extensionsrust-analyzer cargo ron 0.12.0 extensions/Extensions#, |
| 13 | serrust-analyzer cargo ron 0.12.0 ser/::{PrettyConfigrust-analyzer cargo ron 0.12.0 ser/PrettyConfig#, Serializerrust-analyzer cargo ron 0.12.0 ser/Serializer#}, |
| 14 | }; |
| 15 | |
| 16 | #[cfg(feature = "std")] |
| 17 | use { |
| 18 | craterust-analyzer cargo ron 0.12.0 crate/::errorrust-analyzer cargo ron 0.12.0 error/::{Positionrust-analyzer cargo ron 0.12.0 error/Position#, Spanrust-analyzer cargo ron 0.12.0 error/Span#, SpannedErrorrust-analyzer cargo ron 0.12.0 error/SpannedError#}, |
| 19 | allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#, |
| 20 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, |
| 21 | }; |
| 22 | |
| 23 | /// Roundtrip serde options. |
| 24 | /// |
| 25 | /// # Examples |
| 26 | /// |
| 27 | /// ``` |
| 28 | /// use ron::{Options, extensions::Extensions}; |
| 29 | /// |
| 30 | /// let ron = Options::default() |
| 31 | /// .with_default_extension(Extensions::IMPLICIT_SOME); |
| 32 | /// |
| 33 | /// let de: Option<i32> = ron.from_str("42").unwrap(); |
| 34 | /// let ser = ron.to_string(&de).unwrap(); |
| 35 | /// |
| 36 | /// assert_eq!(ser, "42"); |
| 37 | /// ``` |
| 38 | #[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:, Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:, Deserializerust-analyzer cargo serde_derive 1.0.228 Deserialize:)] // GRCOV_EXCL_LINE |
| 39 | #[serde(default)] |
| 40 | #[non_exhaustive] |
| 41 | pub struct Optionsrust-analyzer cargo ron 0.12.0 options/Options# { |
| 42 | /// Extensions that are enabled by default during serialization and |
| 43 | /// deserialization. |
| 44 | /// During serialization, these extensions do NOT have to be explicitly |
| 45 | /// enabled in the parsed RON. |
| 46 | /// During deserialization, these extensions are used, but their explicit |
| 47 | /// activation is NOT included in the output RON. |
| 48 | /// No extensions are enabled by default. |
| 49 | pub default_extensionsrust-analyzer cargo ron 0.12.0 options/Options#default_extensions.: Extensionsrust-analyzer cargo ron 0.12.0 extensions/Extensions#, |
| 50 | /// Default recursion limit that is checked during serialization and |
| 51 | /// deserialization. |
| 52 | /// If set to `None`, infinite recursion is allowed and stack overflow |
| 53 | /// errors can crash the serialization or deserialization process. |
| 54 | /// Defaults to `Some(128)`, i.e. 128 recursive calls are allowed. |
| 55 | pub recursion_limitrust-analyzer cargo ron 0.12.0 options/Options#recursion_limit.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, |
| 56 | } |
| 57 | |
| 58 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Optionsrust-analyzer cargo ron 0.12.0 options/Options# { |
| 59 | fn defaultrust-analyzer cargo ron 0.12.0 options/impl#[Options][Default]default().() -> Selfrust-analyzer cargo ron 0.12.0 options/impl#[Options][Default] { |
| 60 | Selfrust-analyzer cargo ron 0.12.0 options/impl#[Options][Default] { |
| 61 | default_extensionsrust-analyzer cargo ron 0.12.0 options/Options#default_extensions.: Extensionsrust-analyzer cargo ron 0.12.0 extensions/Extensions#::emptyrust-analyzer cargo ron 0.12.0 extensions/impl#[Extensions]empty().(), |
| 62 | recursion_limitrust-analyzer cargo ron 0.12.0 options/Options#recursion_limit.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(128), |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl Optionsrust-analyzer cargo ron 0.12.0 options/Options# { |
| 68 | #[must_use] |
| 69 | /// Enable `default_extension` by default during serialization and deserialization. |
| 70 | pub fn with_default_extensionrust-analyzer cargo ron 0.12.0 options/impl#[Options]with_default_extension().(mut self, default_extension: Extensionsrust-analyzer cargo ron 0.12.0 extensions/Extensions#) -> Selfrust-analyzer cargo ron 0.12.0 options/impl#[Options] { |
| 71 | self.default_extensionsrust-analyzer cargo ron 0.12.0 options/Options#default_extensions. rust-analyzer cargo ron 0.12.0 extensions/impl#[Extensions][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo ron 0.12.0 extensions/impl#[Extensions][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo ron 0.12.0 extensions/impl#[Extensions][`BitOrAssign<Self>`]bitor_assign().default_extension; |
| 72 | self |
| 73 | } |
| 74 | |
| 75 | #[must_use] |
| 76 | /// Do NOT enable `default_extension` by default during serialization and deserialization. |
| 77 | pub fn without_default_extensionrust-analyzer cargo ron 0.12.0 options/impl#[Options]without_default_extension().(mut self, default_extension: Extensionsrust-analyzer cargo ron 0.12.0 extensions/Extensions#) -> Selfrust-analyzer cargo ron 0.12.0 options/impl#[Options] { |
| 78 | self.default_extensionsrust-analyzer cargo ron 0.12.0 options/Options#default_extensions. rust-analyzer cargo ron 0.12.0 extensions/impl#[Extensions][`BitAndAssign<Self>`]bitand_assign().&=rust-analyzer cargo ron 0.12.0 extensions/impl#[Extensions][`BitAndAssign<Self>`]bitand_assign(). rust-analyzer cargo ron 0.12.0 extensions/impl#[Extensions][`BitAndAssign<Self>`]bitand_assign().!rust-analyzer cargo ron 0.12.0 extensions/impl#[Extensions][Not]not().default_extension; |
| 79 | self |
| 80 | } |
| 81 | |
| 82 | #[must_use] |
| 83 | /// Set a maximum recursion limit during serialization and deserialization. |
| 84 | pub fn with_recursion_limitrust-analyzer cargo ron 0.12.0 options/impl#[Options]with_recursion_limit().(mut self, recursion_limit: usize) -> Selfrust-analyzer cargo ron 0.12.0 options/impl#[Options] { |
| 85 | self.recursion_limitrust-analyzer cargo ron 0.12.0 options/Options#recursion_limit. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(recursion_limit); |
| 86 | self |
| 87 | } |
| 88 | |
| 89 | #[must_use] |
| 90 | /// Disable the recursion limit during serialization and deserialization. |
| 91 | /// |
| 92 | /// If you expect to handle highly recursive datastructures, consider wrapping |
| 93 | /// `ron` with [`serde_stacker`](https://docs.rs/serde_stacker/latest/serde_stacker/). |
| 94 | pub fn without_recursion_limitrust-analyzer cargo ron 0.12.0 options/impl#[Options]without_recursion_limit().(mut self) -> Selfrust-analyzer cargo ron 0.12.0 options/impl#[Options] { |
| 95 | self.recursion_limitrust-analyzer cargo ron 0.12.0 options/Options#recursion_limit. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 96 | self |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl Optionsrust-analyzer cargo ron 0.12.0 options/Options# { |
| 101 | /// A convenience function for building a deserializer |
| 102 | /// and deserializing a value of type `T` from a reader. |
| 103 | #[cfg(feature = "std")] |
| 104 | pub fn from_readerrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_reader().<R, T>(&self, rdr: R) -> SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<T> |
| 105 | where |
| 106 | R: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, |
| 107 | T: derust-analyzer cargo serde_core 1.0.228 de/::DeserializeOwnedrust-analyzer cargo serde_core 1.0.228 de/DeserializeOwned#, |
| 108 | { |
| 109 | self.from_reader_seedrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_reader_seed().(rdr, corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#) |
| 110 | } |
| 111 | |
| 112 | /// A convenience function for building a deserializer |
| 113 | /// and deserializing a value of type `T` from a string. |
| 114 | pub fn from_strrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_str().<'a, T>(&self, s: &'a str) -> SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<T> |
| 115 | where |
| 116 | T: derust-analyzer cargo serde_core 1.0.228 de/::Deserializerust-analyzer cargo serde_core 1.0.228 de/Deserialize#<'a>, |
| 117 | { |
| 118 | self.from_str_seedrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_str_seed().(s, corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#) |
| 119 | } |
| 120 | |
| 121 | /// A convenience function for building a deserializer |
| 122 | /// and deserializing a value of type `T` from bytes. |
| 123 | pub fn from_bytesrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_bytes().<'a, T>(&self, s: &'a [u8]) -> SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<T> |
| 124 | where |
| 125 | T: derust-analyzer cargo serde_core 1.0.228 de/::Deserializerust-analyzer cargo serde_core 1.0.228 de/Deserialize#<'a>, |
| 126 | { |
| 127 | self.from_bytes_seedrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_bytes_seed().(s, corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#) |
| 128 | } |
| 129 | |
| 130 | /// A convenience function for building a deserializer |
| 131 | /// and deserializing a value of type `T` from a reader |
| 132 | /// and a seed. |
| 133 | // FIXME: panic is not actually possible, remove once utf8_chunks is stabilized |
| 134 | #[allow(clippy::missing_panics_doc)] |
| 135 | #[cfg(feature = "std")] |
| 136 | pub fn from_reader_seedrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_reader_seed().<R, S, T>(&self, mut rdr: R, seed: S) -> SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<T> |
| 137 | where |
| 138 | R: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, |
| 139 | S: for<'a> derust-analyzer cargo serde_core 1.0.228 de/::DeserializeSeedrust-analyzer cargo serde_core 1.0.228 de/DeserializeSeed#<'a, Valuerust-analyzer cargo serde_core 1.0.228 de/DeserializeSeed#Value# = T>, |
| 140 | { |
| 141 | let mut bytes = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 142 | |
| 143 | let io_err = if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = rdr.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut bytes) { |
| 144 | err |
| 145 | } else { |
| 146 | return self.from_bytes_seedrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_bytes_seed().(&bytes, seed); |
| 147 | }; |
| 148 | |
| 149 | // Try to compute a good error position for the I/O error |
| 150 | // FIXME: use [`utf8_chunks`](https://github.com/rust-lang/rust/issues/99543) once stabilised |
| 151 | #[allow(clippy::expect_used)] |
| 152 | let valid_input = match corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&bytes) { |
| 153 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(valid_input) => valid_input, |
| 154 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&bytes[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#err.valid_up_torust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/impl#[Utf8Error]valid_up_to().()]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().) |
| 155 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("source is valid up to error"), |
| 156 | }; |
| 157 | |
| 158 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SpannedErrorrust-analyzer cargo ron 0.12.0 error/SpannedError# { |
| 159 | coderust-analyzer cargo ron 0.12.0 error/SpannedError#code.: io_err.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 160 | spanrust-analyzer cargo ron 0.12.0 error/SpannedError#span.: Spanrust-analyzer cargo ron 0.12.0 error/Span# { |
| 161 | startrust-analyzer cargo ron 0.12.0 error/Span#start.: Positionrust-analyzer cargo ron 0.12.0 error/Position# { linerust-analyzer cargo ron 0.12.0 error/Position#line.: 1, colrust-analyzer cargo ron 0.12.0 error/Position#col.: 1 }, |
| 162 | endrust-analyzer cargo ron 0.12.0 error/Span#end.: Positionrust-analyzer cargo ron 0.12.0 error/Position#::from_src_endrust-analyzer cargo ron 0.12.0 error/impl#[Position]from_src_end().(valid_input), |
| 163 | }, |
| 164 | }) |
| 165 | } |
| 166 | |
| 167 | /// A convenience function for building a deserializer |
| 168 | /// and deserializing a value of type `T` from a string |
| 169 | /// and a seed. |
| 170 | pub fn from_str_seedrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_str_seed().<'a, S, T>(&self, s: &'a str, seed: S) -> SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<T> |
| 171 | where |
| 172 | S: derust-analyzer cargo serde_core 1.0.228 de/::DeserializeSeedrust-analyzer cargo serde_core 1.0.228 de/DeserializeSeed#<'a, Valuerust-analyzer cargo serde_core 1.0.228 de/DeserializeSeed#Value# = T>, |
| 173 | { |
| 174 | let mut deserializer = Deserializerrust-analyzer cargo ron 0.12.0 de/Deserializer#::from_str_with_optionsrust-analyzer cargo ron 0.12.0 de/impl#[`Deserializer<'de>`]from_str_with_options().(s, self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 175 | |
| 176 | let value = seed |
| 177 | .deserializerust-analyzer cargo serde_core 1.0.228 de/DeserializeSeed#deserialize().(&mut deserializer) |
| 178 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| deserializer.span_errorrust-analyzer cargo ron 0.12.0 de/impl#[`Deserializer<'de>`]span_error().(e))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 179 | |
| 180 | deserializer.endrust-analyzer cargo ron 0.12.0 de/impl#[`Deserializer<'de>`]end().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| deserializer.span_errorrust-analyzer cargo ron 0.12.0 de/impl#[`Deserializer<'de>`]span_error().(e))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 181 | |
| 182 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value) |
| 183 | } |
| 184 | |
| 185 | /// A convenience function for building a deserializer |
| 186 | /// and deserializing a value of type `T` from bytes |
| 187 | /// and a seed. |
| 188 | pub fn from_bytes_seedrust-analyzer cargo ron 0.12.0 options/impl#[Options]from_bytes_seed().<'a, S, T>(&self, s: &'a [u8], seed: S) -> SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<T> |
| 189 | where |
| 190 | S: derust-analyzer cargo serde_core 1.0.228 de/::DeserializeSeedrust-analyzer cargo serde_core 1.0.228 de/DeserializeSeed#<'a, Valuerust-analyzer cargo serde_core 1.0.228 de/DeserializeSeed#Value# = T>, |
| 191 | { |
| 192 | let mut deserializer = Deserializerrust-analyzer cargo ron 0.12.0 de/Deserializer#::from_bytes_with_optionsrust-analyzer cargo ron 0.12.0 de/impl#[`Deserializer<'de>`]from_bytes_with_options().(s, self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 193 | |
| 194 | let value = seed |
| 195 | .deserializerust-analyzer cargo serde_core 1.0.228 de/DeserializeSeed#deserialize().(&mut deserializer) |
| 196 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| deserializer.span_errorrust-analyzer cargo ron 0.12.0 de/impl#[`Deserializer<'de>`]span_error().(e))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 197 | |
| 198 | deserializer.endrust-analyzer cargo ron 0.12.0 de/impl#[`Deserializer<'de>`]end().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| deserializer.span_errorrust-analyzer cargo ron 0.12.0 de/impl#[`Deserializer<'de>`]span_error().(e))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 199 | |
| 200 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value) |
| 201 | } |
| 202 | |
| 203 | /// Serializes `value` into `writer`. |
| 204 | /// |
| 205 | /// This function does not generate any newlines or nice formatting; |
| 206 | /// if you want that, you can use |
| 207 | /// [`to_writer_pretty`][Self::to_writer_pretty] instead. |
| 208 | pub fn to_writerrust-analyzer cargo ron 0.12.0 options/impl#[Options]to_writer().<W, T>(&self, writer: W, value: &T) -> Resultrust-analyzer cargo ron 0.12.0 error/Result#<()> |
| 209 | where |
| 210 | W: fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#, |
| 211 | T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + serrust-analyzer cargo serde_core 1.0.228 ser/::Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#, |
| 212 | { |
| 213 | let mut s = Serializerrust-analyzer cargo ron 0.12.0 ser/Serializer#::with_optionsrust-analyzer cargo ron 0.12.0 ser/impl#[`Serializer<W>`]with_options().(writer, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 214 | value.serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#serialize().(&mut s) |
| 215 | } |
| 216 | |
| 217 | /// Serializes `value` into `writer` in a pretty way. |
| 218 | pub fn to_writer_prettyrust-analyzer cargo ron 0.12.0 options/impl#[Options]to_writer_pretty().<W, T>(&self, writer: W, value: &T, config: PrettyConfigrust-analyzer cargo ron 0.12.0 ser/PrettyConfig#) -> Resultrust-analyzer cargo ron 0.12.0 error/Result#<()> |
| 219 | where |
| 220 | W: fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#, |
| 221 | T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + serrust-analyzer cargo serde_core 1.0.228 ser/::Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#, |
| 222 | { |
| 223 | let mut s = Serializerrust-analyzer cargo ron 0.12.0 ser/Serializer#::with_optionsrust-analyzer cargo ron 0.12.0 ser/impl#[`Serializer<W>`]with_options().(writer, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(config), self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 224 | value.serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#serialize().(&mut s) |
| 225 | } |
| 226 | |
| 227 | /// Serializes `value` into `writer`. |
| 228 | /// |
| 229 | /// This function does not generate any newlines or nice formatting; |
| 230 | /// if you want that, you can use |
| 231 | /// [`to_io_writer_pretty`][Self::to_io_writer_pretty] instead. |
| 232 | #[cfg(feature = "std")] |
| 233 | pub fn to_io_writerrust-analyzer cargo ron 0.12.0 options/impl#[Options]to_io_writer().<W, T>(&self, writer: W, value: &T) -> Resultrust-analyzer cargo ron 0.12.0 error/Result#<()> |
| 234 | where |
| 235 | W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 236 | T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + serrust-analyzer cargo serde_core 1.0.228 ser/::Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#, |
| 237 | { |
| 238 | let mut adapter = Adapterrust-analyzer cargo ron 0.12.0 options/Adapter# { |
| 239 | writerrust-analyzer cargo ron 0.12.0 options/Adapter#writer., |
| 240 | errorrust-analyzer cargo ron 0.12.0 options/Adapter#error.: Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 241 | }; |
| 242 | let result = self.to_writerrust-analyzer cargo ron 0.12.0 options/impl#[Options]to_writer().(&mut adapter, value); |
| 243 | adapter.errorrust-analyzer cargo ron 0.12.0 options/Adapter#error.?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 244 | result |
| 245 | } |
| 246 | |
| 247 | /// Serializes `value` into `writer` in a pretty way. |
| 248 | #[cfg(feature = "std")] |
| 249 | pub fn to_io_writer_prettyrust-analyzer cargo ron 0.12.0 options/impl#[Options]to_io_writer_pretty().<W, T>( |
| 250 | &self, |
| 251 | writer: W, |
| 252 | value: &T, |
| 253 | config: PrettyConfigrust-analyzer cargo ron 0.12.0 ser/PrettyConfig#, |
| 254 | ) -> Resultrust-analyzer cargo ron 0.12.0 error/Result#<()> |
| 255 | where |
| 256 | W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 257 | T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + serrust-analyzer cargo serde_core 1.0.228 ser/::Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#, |
| 258 | { |
| 259 | let mut adapter = Adapterrust-analyzer cargo ron 0.12.0 options/Adapter# { |
| 260 | writerrust-analyzer cargo ron 0.12.0 options/Adapter#writer., |
| 261 | errorrust-analyzer cargo ron 0.12.0 options/Adapter#error.: Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 262 | }; |
| 263 | let result = self.to_writer_prettyrust-analyzer cargo ron 0.12.0 options/impl#[Options]to_writer_pretty().(&mut adapter, value, config); |
| 264 | adapter.errorrust-analyzer cargo ron 0.12.0 options/Adapter#error.?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 265 | result |
| 266 | } |
| 267 | |
| 268 | /// Serializes `value` and returns it as string. |
| 269 | /// |
| 270 | /// This function does not generate any newlines or nice formatting; |
| 271 | /// if you want that, you can use |
| 272 | /// [`to_string_pretty`][Self::to_string_pretty] instead. |
| 273 | pub fn to_stringrust-analyzer cargo ron 0.12.0 options/impl#[Options]to_string().<T>(&self, value: &T) -> Resultrust-analyzer cargo ron 0.12.0 error/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 274 | where |
| 275 | T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + serrust-analyzer cargo serde_core 1.0.228 ser/::Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#, |
| 276 | { |
| 277 | let mut output = 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().(); |
| 278 | let mut s = Serializerrust-analyzer cargo ron 0.12.0 ser/Serializer#::with_optionsrust-analyzer cargo ron 0.12.0 ser/impl#[`Serializer<W>`]with_options().(&mut output, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 279 | value.serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#serialize().(&mut s)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 280 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(output) |
| 281 | } |
| 282 | |
| 283 | /// Serializes `value` in the recommended RON layout in a pretty way. |
| 284 | pub fn to_string_prettyrust-analyzer cargo ron 0.12.0 options/impl#[Options]to_string_pretty().<T>(&self, value: &T, config: PrettyConfigrust-analyzer cargo ron 0.12.0 ser/PrettyConfig#) -> Resultrust-analyzer cargo ron 0.12.0 error/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 285 | where |
| 286 | T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + serrust-analyzer cargo serde_core 1.0.228 ser/::Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#, |
| 287 | { |
| 288 | let mut output = 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().(); |
| 289 | let mut s = Serializerrust-analyzer cargo ron 0.12.0 ser/Serializer#::with_optionsrust-analyzer cargo ron 0.12.0 ser/impl#[`Serializer<W>`]with_options().(&mut output, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(config), self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 290 | value.serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#serialize().(&mut s)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 291 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(output) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Adapter from io::Write to fmt::Write that keeps the error |
| 296 | #[cfg(feature = "std")] |
| 297 | struct Adapterrust-analyzer cargo ron 0.12.0 options/Adapter#<W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#> { |
| 298 | writerrust-analyzer cargo ron 0.12.0 options/Adapter#writer.: W, |
| 299 | errorrust-analyzer cargo ron 0.12.0 options/Adapter#error.: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()>, |
| 300 | } |
| 301 | |
| 302 | #[cfg(feature = "std")] |
| 303 | impl<T: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write# for Adapterrust-analyzer cargo ron 0.12.0 options/Adapter#<T> { |
| 304 | fn write_strrust-analyzer cargo ron 0.12.0 options/impl#[`Adapter<T>`][Write]write_str().(&mut self, s: &str) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 305 | match self.writerrust-analyzer cargo ron 0.12.0 options/Adapter#writer..write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(s.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()) { |
| 306 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 307 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => { |
| 308 | self.errorrust-analyzer cargo ron 0.12.0 options/Adapter#error. = Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e); |
| 309 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#) |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |