@@ -16,17 +16,17 @@ limitations under the License.
1616
1717use core:: fmt;
1818use core:: result:: Result ;
19- use core:: ffi:: { CStr , FromBytesUntilNulError } ;
2019
21- /// FixedStringBuf is a buffer that can hold a fixed-size string.
20+
21+ /// FixedStringBuf is a buffer that can hold a fixed-size string of capacity N.
2222/// It is meant to be used with a slice that the user has pre-allocated
2323/// to avoid extra allocations during string formatting.
24- pub struct FixedStringBuf < ' a > {
25- pub buf : & ' a mut [ u8 ] ,
24+ pub struct FixedStringBuf < const N : usize > {
25+ pub buf : [ u8 ; N ] ,
2626 pub pos : usize ,
2727}
2828
29- impl < ' a > fmt:: Write for FixedStringBuf < ' a > {
29+ impl < ' a , const N : usize > fmt:: Write for FixedStringBuf < N > {
3030 fn write_str ( & mut self , s : & str ) -> fmt:: Result {
3131 // we always reserve 1 byte for the null terminator,
3232 // as the buffer must be convertible to CStr.
@@ -41,49 +41,53 @@ impl<'a> fmt::Write for FixedStringBuf<'a> {
4141 }
4242}
4343
44- impl < ' a > FixedStringBuf < ' a > {
44+ impl < const N : usize > FixedStringBuf < N > {
4545 pub fn as_str ( & self ) -> Result < & str , core:: str:: Utf8Error > {
4646 core:: str:: from_utf8 ( & self . buf [ ..self . pos ] )
4747 }
4848
49- pub fn new ( buf : & ' a mut [ u8 ] ) -> FixedStringBuf < ' a > {
50- assert ! ( buf. len( ) > 0 ) ;
51- FixedStringBuf {
52- buf,
49+ pub const fn new ( ) -> Self {
50+ return FixedStringBuf {
51+ buf : [ 0u8 ; N ] ,
5352 pos : 0 ,
5453 }
5554 }
5655
57- pub fn reset ( & mut self ) {
58- self . pos = 0 ;
59- }
60-
6156 /// Null terminates the underlying buffer,
6257 /// and converts to a CStr which borrows the underlying buffer's slice.
63- pub fn as_c_str ( & mut self ) -> Result < & CStr , FromBytesUntilNulError > {
58+ pub fn as_c_str ( & mut self ) -> Result < & core :: ffi :: CStr , core :: ffi :: FromBytesUntilNulError > {
6459 // null terminate the buffer.
6560 // we are guaranteed to have enough space since we always reserve one extra
6661 // byte for null in write_str, and assert buf.len() > 0 in the constructor.
6762 assert ! ( self . buf. len( ) > 0 && self . pos < self . buf. len( ) ) ;
6863 self . buf [ self . pos ] = 0 ;
69- CStr :: from_bytes_until_nul ( & self . buf [ ..self . pos + 1 ] )
64+ core :: ffi :: CStr :: from_bytes_until_nul ( & self . buf [ ..self . pos + 1 ] )
7065 }
7166}
7267
68+
7369mod test {
74-
75-
76-
70+ // disable unused import warnings
71+ #![ allow( unused_imports) ]
72+ use core:: fmt:: Write ;
73+ use core:: fmt;
74+ use super :: FixedStringBuf ;
75+
7776 #[ test]
7877 fn test_fixed_buf ( ) {
79- let mut bs = [ 0 ; 21 ] ;
80- let mut buf = FixedStringBuf :: new ( & mut bs) ;
78+ let mut buf = FixedStringBuf :: < 21 > :: new ( ) ;
79+
80+ assert_eq ! ( buf. as_str( ) . unwrap( ) , "" ) ;
81+
8182 write ! ( & mut buf, "{}" , "0123456789" ) . expect ( "Failed to write to FixedBuf" ) ;
8283 write ! ( & mut buf, "{}" , "0123456789" ) . expect ( "Failed to write to FixedBuf" ) ;
8384 assert_eq ! ( buf. as_str( ) . unwrap( ) , "01234567890123456789" ) ;
8485 assert_eq ! ( buf. pos, 20 ) ;
8586
8687 let res = write ! ( & mut buf, "10" ) ;
8788 assert_eq ! ( res, Err ( fmt:: Error ) ) ;
89+
90+ let c_str = buf. as_c_str ( ) . unwrap ( ) ;
91+ assert_eq ! ( c_str. to_bytes( ) , b"01234567890123456789" ) ;
8892 }
8993}
0 commit comments