@@ -6,6 +6,7 @@ use crate::alloc::{flags::*, AllocError, KVec};
66use crate :: prelude:: * ;
77use core:: {
88 fmt:: { self , Write } ,
9+ marker:: PhantomData ,
910 ops:: { self , Deref , DerefMut , Index } ,
1011} ;
1112
@@ -702,7 +703,7 @@ mod tests {
702703///
703704/// The memory region between `pos` (inclusive) and `end` (exclusive) is valid for writes if `pos`
704705/// is less than `end`.
705- pub ( crate ) struct RawFormatter {
706+ pub struct RawFormatter {
706707 // Use `usize` to use `saturating_*` functions.
707708 beg : usize ,
708709 pos : usize ,
@@ -760,7 +761,7 @@ impl RawFormatter {
760761 }
761762
762763 /// Returns the number of bytes written to the formatter.
763- pub ( crate ) fn bytes_written ( & self ) -> usize {
764+ pub fn bytes_written ( & self ) -> usize {
764765 self . pos - self . beg
765766 }
766767}
@@ -794,7 +795,7 @@ impl fmt::Write for RawFormatter {
794795/// Allows formatting of [`fmt::Arguments`] into a raw buffer.
795796///
796797/// Fails if callers attempt to write more than will fit in the buffer.
797- pub ( crate ) struct Formatter ( RawFormatter ) ;
798+ pub struct Formatter ( RawFormatter ) ;
798799
799800impl Formatter {
800801 /// Creates a new instance of [`Formatter`] with the given buffer.
@@ -830,6 +831,35 @@ impl fmt::Write for Formatter {
830831 }
831832}
832833
834+ /// A mutable reference to a byte buffer where a string can be written into.
835+ pub struct BorrowFormatter < ' a > ( Formatter , PhantomData < & ' a mut ( ) > ) ;
836+
837+ impl < ' a > BorrowFormatter < ' a > {
838+ /// Create a new [`Self`] instance.
839+ pub fn new ( buffer : & ' a mut [ u8 ] ) -> Result < BorrowFormatter < ' a > > {
840+ Ok ( Self (
841+ // SAFETY: `buffer` is valid for writes for the entire length for
842+ // the lifetime of `Self`.
843+ unsafe { Formatter :: from_buffer ( buffer. as_mut_ptr ( ) , buffer. len ( ) ) } ,
844+ PhantomData ,
845+ ) )
846+ }
847+ }
848+
849+ impl Deref for BorrowFormatter < ' _ > {
850+ type Target = Formatter ;
851+
852+ fn deref ( & self ) -> & Self :: Target {
853+ & self . 0
854+ }
855+ }
856+
857+ impl DerefMut for BorrowFormatter < ' _ > {
858+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
859+ & mut self . 0
860+ }
861+ }
862+
833863/// An owned string that is guaranteed to have exactly one `NUL` byte, which is at the end.
834864///
835865/// Used for interoperability with kernel APIs that take C strings.
0 commit comments