usecrate::error::{Error, Result}; use half::f16; use serde::ser::{self, Serialize}; #[cfg(feature = "std")] use std::io;
usecrate::tags::{get_tag, CBOR_NEWTYPE_NAME};
/// Serializes a value to a vector. #[cfg(any(feature = "std", feature = "alloc"))] pubfn to_vec<T>(value: &T) -> Result<Vec<u8>> where
T: ser::Serialize,
{ letmut vec = Vec::new();
value.serialize(&mut Serializer::new(&>mut vec))?;
Ok(vec)
}
/// Serializes a value to a vector in packed format. #[cfg(feature = "std")] pubfn to_vec_packed<T>(value: &T) -> Result<Vec<u8>> where
T: ser::Serialize,
{ letmut vec = Vec::new();
value.serialize(&mut Serializer::new(&>mut IoWrite::new(&mut vec)).packed_format())?;
Ok(vec)
}
/// Serializes a value to a writer. #[cfg(feature = "std")] pubfn to_writer<W, T>(writer: W, value: &T) -> Result<()> where
W: io::Write,
T: ser::Serialize,
{
value.serialize(&mut Serializer::new(&>mut IoWrite::new(writer)))
}
/// A structure for serializing Rust values to CBOR. #[derive(Debug)] pubstruct Serializer<W> {
writer: W,
packed: bool,
enum_as_map: bool,
}
impl<W> Serializer<W> where
W: Write,
{ /// Creates a new CBOR serializer. /// /// `to_vec` and `to_writer` should normally be used instead of this method. #[inline] pubfn new(writer: W) -> Self {
Serializer {
writer,
packed: false,
enum_as_map: true,
}
}
/// Choose concise/packed format for serializer. /// /// In the packed format enum variant names and field names /// are replaced with numeric indizes to conserve space. pubfn packed_format(mutself) -> Self { self.packed = true; self
}
/// Enable old enum format used by `serde_cbor` versions <= v0.9. /// /// The `legacy_enums` option determines how enums are encoded. /// /// This makes no difference when encoding and decoding enums using /// this crate, but it shows up when decoding to a `Value` or decoding /// in other languages. /// /// # Examples /// /// Given the following enum /// /// ```rust /// enum Enum { /// Unit, /// NewType(i32), /// Tuple(String, bool), /// Struct{ x: i32, y: i32 }, /// } /// ``` /// we will give the `Value` with the same encoding for each case using /// JSON notation. /// /// ## Default encodings /// /// * `Enum::Unit` encodes as `"Unit"` /// * `Enum::NewType(10)` encodes as `{"NewType": 10}` /// * `Enum::Tuple("x", true)` encodes as `{"Tuple": ["x", true]}` /// /// ## Legacy encodings /// /// * `Enum::Unit` encodes as `"Unit"` /// * `Enum::NewType(10)` encodes as `["NewType", 10]` /// * `Enum::Tuple("x", true)` encodes as `["Tuple", "x", true]` /// * `Enum::Struct{ x: 5, y: -5 }` encodes as `["Struct", {"x": 5, "y": -5}]` pubfn legacy_enums(mutself) -> Self { self.enum_as_map = false; self
}
/// Writes a CBOR self-describe tag to the stream. /// /// Tagging allows a decoder to distinguish different file formats based on their content /// without further information. #[inline] pubfn self_describe(&mutself) -> Result<()> { letmut buf = [6 << 5 | 25, 0, 0];
(&mut buf[1..]).copy_from_slice(&55799u16.to_be_bytes()); self.writer.write_all(&buf).map_err(|e| e.into())
}
/// Unwrap the `Writer` from the `Serializer`. #[inline] pubfn into_inner(self) -> W { self.writer
}
impl<'a, W> ser::Serializer for &'a mut Serializer<W> where
W: Write,
{ type Ok = (); type Error = Error;
type SerializeSeq = CollectionSerializer<'a, W>; type SerializeTuple = &'a mut Serializer<W>; type SerializeTupleStruct = &'a mut Serializer<W>; type SerializeTupleVariant = &'a mut Serializer<W>; type SerializeMap = CollectionSerializer<'a, W>; type SerializeStruct = StructSerializer<'a, W>; type SerializeStructVariant = StructSerializer<'a, W>;
#[inline] fn serialize_bool(self, value: bool) -> Result<()> { let value = if value { 0xf5 } else { 0xf4 }; self.writer.write_all(&[value]).map_err(|e| e.into())
}
#[inline] fn serialize_i8(self, value: i8) -> Result<()> { if value < 0 { self.write_u8(1, -(value + 1) as u8)
} else { self.write_u8(0, value as u8)
}
}
#[inline] fn serialize_i16(self, value: i16) -> Result<()> { if value < 0 { self.write_u16(1, -(value + 1) as u16)
} else { self.write_u16(0, value as u16)
}
}
#[inline] fn serialize_i32(self, value: i32) -> Result<()> { if value < 0 { self.write_u32(1, -(value + 1) as u32)
} else { self.write_u32(0, value as u32)
}
}
#[inline] fn serialize_i64(self, value: i64) -> Result<()> { if value < 0 { self.write_u64(1, -(value + 1) as u64)
} else { self.write_u64(0, value as u64)
}
}
#[inline] fn serialize_i128(self, value: i128) -> Result<()> { if value < 0 { if -(value + 1) > i128::from(u64::max_value()) { return Err(Error::message("The number can't be stored in CBOR"));
} self.write_u64(1, -(value + 1) as u64)
} else { if value > i128::from(u64::max_value()) { return Err(Error::message("The number can't be stored in CBOR"));
} self.write_u64(0, value as u64)
}
}
#[inline] fn serialize_u128(self, value: u128) -> Result<()> { if value > u128::from(u64::max_value()) { return Err(Error::message("The number can't be stored in CBOR"));
} self.write_u64(0, value as u64)
}
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.