usesuper::style::Style; usesuper::Error; usecrate::alloc_prelude::*; #[allow(clippy::wildcard_imports)] pub(crate) use array::*; #[allow(clippy::wildcard_imports)] pub(crate) use key::*; #[allow(clippy::wildcard_imports)] pub(crate) use map::*;
/// Serialization for TOML [values][crate::Value]. /// /// This structure implements serialization support for TOML to serialize an /// arbitrary type to TOML. Note that the TOML format does not support all /// datatypes in Rust, such as enums, tuples, and tuple structs. These types /// will generate an error when serialized. /// /// Currently a serializer always writes its output to an in-memory `String`, /// which is passed in when creating the serializer itself. /// /// # Examples /// /// ``` /// use serde::Serialize; /// /// #[derive(Serialize)] /// struct Config { /// database: Database, /// } /// /// #[derive(Serialize)] /// struct Database { /// ip: String, /// port: Vec<u16>, /// connection_max: u32, /// enabled: bool, /// } /// /// let config = Config { /// database: Database { /// ip: "192.168.1.1".to_string(), /// port: vec![8001, 8002, 8003], /// connection_max: 5000, /// enabled: false, /// }, /// }; /// /// let mut value = String::new(); /// serde::Serialize::serialize( /// &config, /// toml::ser::ValueSerializer::new(&mut value) /// ).unwrap(); /// println!("{}", value) /// ``` pubstruct ValueSerializer<'d> {
dst: &'d mut String,
style: Style,
}
impl<'d> ValueSerializer<'d> { /// Creates a new serializer which will emit TOML into the buffer provided. /// /// The serializer can then be used to serialize a type after which the data /// will be present in `dst`. pubfn new(dst: &'d mut String) -> Self { Self {
dst,
style: Default::default(),
}
}
impl<'d> serde_core::ser::Serializer for ValueSerializer<'d> { type Ok = &'d mut String; type Error = Error; type SerializeSeq = SerializeValueArray<'d>; type SerializeTuple = SerializeValueArray<'d>; type SerializeTupleStruct = SerializeValueArray<'d>; type SerializeTupleVariant = SerializeTupleVariant<'d>; type SerializeMap = SerializeMap<'d>; type SerializeStruct = SerializeMap<'d>; type SerializeStructVariant = SerializeStructVariant<'d>;
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { let v: i64 = v
.try_into()
.map_err(|_err| Error::out_of_range(Some("u64")))?; self.serialize_i64(v)
}
fn serialize_f32(self, mut v: f32) -> Result<Self::Ok, Self::Error> { // Discard sign of NaN when serialized using Serde. // // In all likelihood the sign of NaNs is not meaningful in the user's // program. Ending up with `-nan` in the TOML document would usually be // surprising and undesirable, when the sign of the NaN was not // intentionally controlled by the caller, or may even be // nondeterministic if it comes from arithmetic operations or a cast. if v.is_nan() {
v = v.copysign(1.0);
} self.dst.value(v)?;
Ok(self.dst)
}
fn serialize_f64(self, mut v: f64) -> Result<Self::Ok, Self::Error> { // Discard sign of NaN when serialized using Serde. // // In all likelihood the sign of NaNs is not meaningful in the user's // program. Ending up with `-nan` in the TOML document would usually be // surprising and undesirable, when the sign of the NaN was not // intentionally controlled by the caller, or may even be // nondeterministic if it comes from arithmetic operations or a cast. if v.is_nan() {
v = v.copysign(1.0);
} self.dst.value(v)?;
Ok(self.dst)
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.