From 1e100a0902e444618c1af9a90684885f7e750d4e Mon Sep 17 00:00:00 2001 From: Aljoscha Meyer Date: Sat, 6 Jul 2024 22:37:29 +0200 Subject: [PATCH] Implement Debug for non-clone Fixed queues --- src/fixed.rs | 84 +++++++++++++++++++++++++++++++++------------------- src/lib.rs | 1 + 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/fixed.rs b/src/fixed.rs index fe36e84..d180fba 100644 --- a/src/fixed.rs +++ b/src/fixed.rs @@ -10,7 +10,7 @@ use crate::Queue; /// A queue holding up to a certain number of items. The capacity is set upon /// creation and remains fixed. Performs a single heap allocation on creation. -/// +/// /// Use the methods of the [Queue] trait implementation to interact with the contents of the queue. pub struct Fixed { /// Slice of memory, used as a ring-buffer. @@ -96,27 +96,6 @@ impl Fixed { } } -impl Fixed { - // For implementing Debug - fn vec_of_current_items(&self) -> alloc::vec::Vec { - if self.is_data_contiguous() { - unsafe { - MaybeUninit::slice_assume_init_ref(&self.data[self.read..self.write_to()]).to_vec() - } - } else { - // We only work with data thas has been enqueued, so the memory is not uninitialized anymore. - unsafe { - let mut ret = MaybeUninit::slice_assume_init_ref(&self.data[self.read..]).to_vec(); - let len_first_slice = ret.len(); - ret.extend_from_slice(MaybeUninit::slice_assume_init_ref( - &self.data[0..(self.amount - len_first_slice)], - )); - ret - } - } - } -} - impl Queue for Fixed { type Item = T; @@ -208,12 +187,39 @@ impl Queue for Fixed { } } -impl fmt::Debug for Fixed { +impl fmt::Debug for Fixed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Fixed") .field("capacity", &self.capacity()) .field("len", &self.amount) - .field("data", &self.vec_of_current_items()) + // .field("data", &self.vec_of_current_items()) + .field_with("data", |f| { + let mut list = f.debug_list(); + + if self.is_data_contiguous() { + for item in unsafe { + MaybeUninit::slice_assume_init_ref(&self.data[self.read..self.write_to()]) + } { + list.entry(item); + } + } else { + for item in + unsafe { MaybeUninit::slice_assume_init_ref(&self.data[self.read..]) } + { + list.entry(item); + } + + for item in unsafe { + MaybeUninit::slice_assume_init_ref( + &self.data[0..(self.amount - self.data[self.read..].len())], + ) + } { + list.entry(item); + } + } + + list.finish() + }) .finish() } } @@ -319,21 +325,39 @@ mod tests { assert_eq!(queue.enqueue(7), None); assert_eq!(queue.enqueue(21), None); assert_eq!(queue.enqueue(196), None); - assert_eq!(format!("{:?}", queue), "Fixed { capacity: 4, len: 3, data: [7, 21, 196] }"); + assert_eq!( + format!("{:?}", queue), + "Fixed { capacity: 4, len: 3, data: [7, 21, 196] }" + ); assert_eq!(queue.dequeue(), Some(7)); - assert_eq!(format!("{:?}", queue), "Fixed { capacity: 4, len: 2, data: [21, 196] }"); + assert_eq!( + format!("{:?}", queue), + "Fixed { capacity: 4, len: 2, data: [21, 196] }" + ); assert_eq!(queue.dequeue(), Some(21)); - assert_eq!(format!("{:?}", queue), "Fixed { capacity: 4, len: 1, data: [196] }"); + assert_eq!( + format!("{:?}", queue), + "Fixed { capacity: 4, len: 1, data: [196] }" + ); assert_eq!(queue.enqueue(33), None); - assert_eq!(format!("{:?}", queue), "Fixed { capacity: 4, len: 2, data: [196, 33] }"); + assert_eq!( + format!("{:?}", queue), + "Fixed { capacity: 4, len: 2, data: [196, 33] }" + ); assert_eq!(queue.enqueue(17), None); - assert_eq!(format!("{:?}", queue), "Fixed { capacity: 4, len: 3, data: [196, 33, 17] }"); + assert_eq!( + format!("{:?}", queue), + "Fixed { capacity: 4, len: 3, data: [196, 33, 17] }" + ); assert_eq!(queue.enqueue(200), None); - assert_eq!(format!("{:?}", queue), "Fixed { capacity: 4, len: 4, data: [196, 33, 17, 200] }"); + assert_eq!( + format!("{:?}", queue), + "Fixed { capacity: 4, len: 4, data: [196, 33, 17, 200] }" + ); } } diff --git a/src/lib.rs b/src/lib.rs index a19abb7..ec56506 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ #![feature(maybe_uninit_uninit_array)] #![feature(maybe_uninit_write_slice)] #![feature(new_uninit)] +#![feature(debug_closure_helpers)] //! A [trait](Queue) and implementations of non-blocking, infallible [FIFO queues](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) that support bulk enqueueing and bulk dequeueing via APIs inspired by [ufotofu](https://crates.io/crates/ufotofu). //!