/// The error type for operations on the [`RmpWrite`] trait. /// /// For [`std::io::Write`], this is [`std::io::Error`] /// For [`ByteBuf`], this is [`core::convert::Infallible`] pubtrait RmpWriteErr: Display + Debug + crate::errors::MaybeErrBound + 'static {} #[cfg(feature = "std")] impl RmpWriteErr for std::io::Error {} impl RmpWriteErr for core::convert::Infallible {}
// An error returned from the `write_marker` and `write_fixval` functions. struct MarkerWriteError<E: RmpWriteErr>(E);
/// Attempts to write the given marker into the writer. fn write_marker<W: RmpWrite>(wr: &mut W, marker: Marker) -> Result<(), MarkerWriteError<W::Error>> {
wr.write_u8(marker.to_u8()).map_err(MarkerWriteError)
}
/// An error returned from primitive values write functions. #[doc(hidden)] pubstruct DataWriteError<E: RmpWriteErr>(E);
/// Encodes and attempts to write a nil value into the given write. /// /// According to the MessagePack specification, a nil value is represented as a single `0xc0` byte. /// /// # Errors /// /// This function will return `Error` on any I/O error occurred while writing the nil marker. /// /// # Examples /// /// ``` /// let mut buf = Vec::new(); /// /// rmp::encode::write_nil(&mut buf).unwrap(); /// /// assert_eq!(vec![0xc0], buf); /// ``` #[inline] pubfn write_nil<W: RmpWrite>(wr: &mut W) -> Result<(), W::Error> {
write_marker(wr, Marker::Null).map_err(|e| e.0)
}
/// Encodes and attempts to write a bool value into the given write. /// /// According to the MessagePack specification, an encoded boolean value is represented as a single /// byte. /// /// # Errors /// /// Each call to this function may generate an I/O error indicating that the operation could not be /// completed. #[inline] pubfn write_bool<W: RmpWrite>(wr: &mut W, val: bool) -> Result<(), W::Error> { let marker = if val { Marker::True } else { Marker::False };
write_marker(wr, marker).map_err(|e| e.0)
}
mod sealed { pubtrait Sealed {} #[cfg(feature = "std")] impl<T: ?Sized + std::io::Write> Sealed for T {} #[cfg(not(feature = "std"))] impl Sealed for &mut [u8] {} #[cfg(not(feature = "std"))] impl Sealed for alloc::vec::Vec<u8> {} impl Sealed forsuper::ByteBuf {}
}
/// A type that `rmp` supports writing into. /// /// The methods of this trait should be considered an implementation detail (for now). /// It is currently sealed (can not be implemented by the user). /// /// See also [`std::io::Write`] and [`byteorder::WriteBytesExt`] /// /// Its primary implementations are [`std::io::Write`] and [`ByteBuf`]. pubtrait RmpWrite: sealed::Sealed { type Error: RmpWriteErr;
/// Write a single byte to this stream #[inline] fn write_u8(&mutself, val: u8) -> Result<(), Self::Error> { let buf = [val]; self.write_bytes(&buf)
}
/// Write a slice of bytes to the underlying stream /// /// This will either write all the bytes or return an error. /// See also [`std::io::Write::write_all`] fn write_bytes(&mutself, buf: &[u8]) -> Result<(), Self::Error>;
// Internal helper functions to map I/O error into the `DataWriteError` error.
/// Write a single (signed) byte to this stream. #[inline] #[doc(hidden)] fn write_data_u8(&mutself, val: u8) -> Result<(), DataWriteError<Self::Error>> { self.write_u8(val).map_err(DataWriteError)
} /// Write a single (signed) byte to this stream. #[inline] #[doc(hidden)] fn write_data_i8(&mutself, val: i8) -> Result<(), DataWriteError<Self::Error>> { self.write_data_u8(val as u8)
}
/// Encodes and attempts to write the most efficient array length implementation to the given write, /// returning the marker used. /// /// # Errors /// /// This function will return `ValueWriteError` on any I/O error occurred while writing either the /// marker or the data. pubfn write_array_len<W: RmpWrite>(wr: &mut W, len: u32) -> Result<Marker, ValueWriteError<W::Error>> { let marker = if len < 16 {
Marker::FixArray(len as u8)
} elseif len <= u16::MAX as u32 {
Marker::Array16
} else {
Marker::Array32
};
write_marker(wr, marker)?; if marker == Marker::Array16 {
wr.write_data_u16(len as u16)?;
} elseif marker == Marker::Array32 {
wr.write_data_u32(len)?;
}
Ok(marker)
}
/// Encodes and attempts to write the most efficient map length implementation to the given write, /// returning the marker used. /// /// # Errors /// /// This function will return `ValueWriteError` on any I/O error occurred while writing either the /// marker or the data. pubfn write_map_len<W: RmpWrite>(wr: &mut W, len: u32) -> Result<Marker, ValueWriteError<W::Error>> { let marker = if len < 16 {
Marker::FixMap(len as u8)
} elseif len <= u16::MAX as u32 {
Marker::Map16
} else {
Marker::Map32
};
write_marker(wr, marker)?; if marker == Marker::Map16 {
wr.write_data_u16(len as u16)?;
} elseif marker == Marker::Map32 {
wr.write_data_u32(len)?;
}
Ok(marker)
}
/// Encodes and attempts to write the most efficient ext metadata implementation to the given /// write, returning the marker used. /// /// # Errors /// /// This function will return `ValueWriteError` on any I/O error occurred while writing either the /// marker or the data. /// /// # Panics /// /// Panics if `ty` is negative, because it is reserved for future MessagePack extension including /// 2-byte type information. pubfn write_ext_meta<W: RmpWrite>(wr: &mut W, len: u32, ty: i8) -> Result<Marker, ValueWriteError<W::Error>> { let marker = match len { 1 => Marker::FixExt1, 2 => Marker::FixExt2, 4 => Marker::FixExt4, 8 => Marker::FixExt8, 16 => Marker::FixExt16, 0..=255 => Marker::Ext8, 256..=65535 => Marker::Ext16,
_ => Marker::Ext32,
};
write_marker(wr, marker)?;
if marker == Marker::Ext8 {
wr.write_data_u8(len as u8)?;
} elseif marker == Marker::Ext16 {
wr.write_data_u16(len as u16)?;
} elseif marker == Marker::Ext32 {
wr.write_data_u32(len)?;
}
wr.write_data_i8(ty)?;
Ok(marker)
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet am 2026-06-18)
¤
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.