Skip to content

Commit 9410343

Browse files
Merge #332
332: Improve `Format` trait docs r=japaric a=jonas-schievink Closes #330 Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents ed955db + 944708c commit 9410343

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ pub trait Write {
620620
fn write(&mut self, bytes: &[u8]);
621621
}
622622

623-
/// Derivable trait for defmt output.
623+
/// Trait for types that can be formatted via defmt.
624624
///
625625
/// This trait is used by the `{:?}` format specifier and can format a wide range of types.
626626
/// User-defined types can `#[derive(Format)]` to get an auto-generated implementation of this
@@ -631,7 +631,7 @@ pub trait Write {
631631
///
632632
/// # Example
633633
///
634-
/// It is required to `#[derive]` implementations of this trait:
634+
/// Usually, an implementation of this trait can be `#[derive]`d automatically:
635635
///
636636
/// ```
637637
/// use defmt::Format;
@@ -643,6 +643,23 @@ pub trait Write {
643643
/// sequence: u16,
644644
/// }
645645
/// ```
646+
///
647+
/// Manual implementations can make use of the [`write!`] macro:
648+
///
649+
/// ```
650+
/// use defmt::{Format, Formatter, write};
651+
///
652+
/// struct Id(u32);
653+
///
654+
/// impl Format for Id {
655+
/// fn format(&self, fmt: Formatter) {
656+
/// // Format as hexadecimal.
657+
/// write!(fmt, "Id({:x})", self.0);
658+
/// }
659+
/// }
660+
/// ```
661+
///
662+
/// Note that [`write!`] can only be called once, as it consumes the [`Formatter`].
646663
pub trait Format {
647664
/// Writes the defmt representation of `self` to `fmt`.
648665
fn format(&self, fmt: Formatter);

0 commit comments

Comments
 (0)