@@ -191,7 +191,14 @@ pub enum Argument {
191191}
192192
193193/// Trait to specify the log behavior for a type.
194- pub trait Log {
194+ ///
195+ /// # Safety
196+ ///
197+ /// The implementation must ensure that the value returned by any of the methods correctly
198+ /// reflects the actual number of bytes written to the buffer. Returning a value greater
199+ /// than the number of bytes written to the buffer will result in undefined behavior, since
200+ /// it will lead to reading uninitialized memory from the buffer.
201+ pub unsafe trait Log {
195202 #[ inline( always) ]
196203 fn debug ( & self , buffer : & mut [ MaybeUninit < u8 > ] ) -> usize {
197204 self . debug_with_args ( buffer, & [ ] )
@@ -213,7 +220,7 @@ pub trait Log {
213220/// Implement the log trait for unsigned integer types.
214221macro_rules! impl_log_for_unsigned_integer {
215222 ( $type: tt, $max_digits: literal ) => {
216- impl Log for $type {
223+ unsafe impl Log for $type {
217224 #[ inline]
218225 fn write_with_args( & self , buffer: & mut [ MaybeUninit <u8 >] , args: & [ Argument ] ) -> usize {
219226 if buffer. is_empty( ) {
@@ -373,7 +380,7 @@ impl_log_for_unsigned_integer!(usize, 20);
373380/// Implement the log trait for the signed integer types.
374381macro_rules! impl_log_for_signed {
375382 ( $type: tt ) => {
376- impl Log for $type {
383+ unsafe impl Log for $type {
377384 #[ inline]
378385 fn write_with_args( & self , buffer: & mut [ MaybeUninit <u8 >] , args: & [ Argument ] ) -> usize {
379386 if buffer. is_empty( ) {
@@ -419,7 +426,7 @@ impl_log_for_signed!(i128);
419426impl_log_for_signed ! ( isize ) ;
420427
421428/// Implement the log trait for the &str type.
422- impl Log for & str {
429+ unsafe impl Log for & str {
423430 #[ inline]
424431 fn debug_with_args ( & self , buffer : & mut [ MaybeUninit < u8 > ] , _args : & [ Argument ] ) -> usize {
425432 if buffer. is_empty ( ) {
@@ -577,15 +584,15 @@ impl Log for &str {
577584/// Implement the log trait for the slice type.
578585macro_rules! impl_log_for_slice {
579586 ( [ $type: ident] ) => {
580- impl <$type> Log for & [ $type]
587+ unsafe impl <$type> Log for & [ $type]
581588 where
582589 $type: Log
583590 {
584591 impl_log_for_slice!( @generate_write) ;
585592 }
586593 } ;
587594 ( [ $type: ident; $size: ident] ) => {
588- impl <$type, const $size: usize > Log for & [ $type; $size]
595+ unsafe impl <$type, const $size: usize > Log for & [ $type; $size]
589596 where
590597 $type: Log
591598 {
@@ -661,7 +668,7 @@ impl_log_for_slice!([T]);
661668impl_log_for_slice ! ( [ T ; N ] ) ;
662669
663670/// Implement the log trait for the bool type.
664- impl Log for bool {
671+ unsafe impl Log for bool {
665672 #[ inline]
666673 fn debug_with_args ( & self , buffer : & mut [ MaybeUninit < u8 > ] , args : & [ Argument ] ) -> usize {
667674 let value = if * self { "true" } else { "false" } ;
0 commit comments