mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 23:01:01 +00:00
Clean up serialize_display_bounded_length
This commit is contained in:
+20
-26
@@ -369,34 +369,30 @@ impl Serialize for Duration {
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// Seralize the `$value` that implements Display as a string,
|
/// Serialize a value that implements `Display` as a string, when that string is
|
||||||
/// when that string is statically known to never have more than
|
/// statically known to never have more than a constant `MAX_LEN` bytes.
|
||||||
/// a constant `$MAX_LEN` bytes.
|
|
||||||
///
|
///
|
||||||
/// Panics if the Display impl tries to write more than `$MAX_LEN` bytes.
|
/// Panics if the `Display` impl tries to write more than `MAX_LEN` bytes.
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
macro_rules! serialize_display_bounded_length {
|
macro_rules! serialize_display_bounded_length {
|
||||||
($value: expr, $MAX_LEN: expr, $serializer: expr) => {
|
($value:expr, $max:expr, $serializer:expr) => {{
|
||||||
{
|
let mut buffer: [u8; $max] = unsafe { mem::uninitialized() };
|
||||||
let mut buffer: [u8; $MAX_LEN] = unsafe { mem::uninitialized() };
|
let remaining_len = {
|
||||||
let remaining_len;
|
let mut remaining = &mut buffer[..];
|
||||||
{
|
write!(remaining, "{}", $value).unwrap();
|
||||||
let mut remaining = &mut buffer[..];
|
remaining.len()
|
||||||
write!(remaining, "{}", $value).unwrap();
|
};
|
||||||
remaining_len = remaining.len()
|
let written_len = buffer.len() - remaining_len;
|
||||||
}
|
let written = &buffer[..written_len];
|
||||||
let written_len = buffer.len() - remaining_len;
|
|
||||||
let written = &buffer[..written_len];
|
|
||||||
|
|
||||||
// write! only provides fmt::Formatter to Display implementations,
|
// write! only provides fmt::Formatter to Display implementations, which
|
||||||
// which has methods write_str and write_char but no method to write arbitrary bytes.
|
// has methods write_str and write_char but no method to write arbitrary
|
||||||
// Therefore, `written` is well-formed in UTF-8.
|
// bytes. Therefore `written` must be valid UTF-8.
|
||||||
let written_str = unsafe {
|
let written_str = unsafe {
|
||||||
str::from_utf8_unchecked(written)
|
str::from_utf8_unchecked(written)
|
||||||
};
|
};
|
||||||
$serializer.serialize_str(written_str)
|
$serializer.serialize_str(written_str)
|
||||||
}
|
}}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
@@ -436,8 +432,6 @@ impl Serialize for net::Ipv6Addr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddr {
|
impl Serialize for net::SocketAddr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
|||||||
Reference in New Issue
Block a user