mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 19:31:02 +00:00
Add format_args example to collect_str
This commit is contained in:
+43
-5
@@ -621,10 +621,29 @@ pub trait Serializer: Sized {
|
|||||||
serializer.end()
|
serializer.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Collect a `Display` value as a string.
|
/// Serialize a string produced by an implementation of `Display`.
|
||||||
///
|
///
|
||||||
/// The default implementation serializes the given value as a string with
|
/// The default implementation builds a heap-allocated `String` and
|
||||||
/// `ToString::to_string`.
|
/// delegates to `serialize_str`. Serializers are encouraged to provide a
|
||||||
|
/// more efficient implementation if possible.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde::{Serialize, Serializer};
|
||||||
|
/// # struct DateTime;
|
||||||
|
/// # impl DateTime {
|
||||||
|
/// # fn naive_local(&self) -> () { () }
|
||||||
|
/// # fn offset(&self) -> () { () }
|
||||||
|
/// # }
|
||||||
|
/// impl Serialize for DateTime {
|
||||||
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
/// where S: Serializer
|
||||||
|
/// {
|
||||||
|
/// serializer.collect_str(&format_args!("{:?}{:?}",
|
||||||
|
/// self.naive_local(),
|
||||||
|
/// self.offset()))
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
where T: Display,
|
where T: Display,
|
||||||
@@ -634,9 +653,28 @@ pub trait Serializer: Sized {
|
|||||||
self.serialize_str(&string)
|
self.serialize_str(&string)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Collect a `Display` value as a string.
|
/// Serialize a string produced by an implementation of `Display`.
|
||||||
///
|
///
|
||||||
/// The default implementation returns an error unconditionally.
|
/// The default implementation returns an error unconditionally when
|
||||||
|
/// compiled with `no_std`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde::{Serialize, Serializer};
|
||||||
|
/// # struct DateTime;
|
||||||
|
/// # impl DateTime {
|
||||||
|
/// # fn naive_local(&self) -> () { () }
|
||||||
|
/// # fn offset(&self) -> () { () }
|
||||||
|
/// # }
|
||||||
|
/// impl Serialize for DateTime {
|
||||||
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
/// where S: Serializer
|
||||||
|
/// {
|
||||||
|
/// serializer.collect_str(&format_args!("{:?}{:?}",
|
||||||
|
/// self.naive_local(),
|
||||||
|
/// self.offset()))
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[cfg(not(any(feature = "std", feature = "collections")))]
|
#[cfg(not(any(feature = "std", feature = "collections")))]
|
||||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
where T: Display,
|
where T: Display,
|
||||||
|
|||||||
Reference in New Issue
Block a user