@@ -28,9 +28,32 @@ use crate::debug::TruncatedDebug;
2828use crate :: trusted_len:: TrustedLen ;
2929
3030/// An immutable buffer of items of `T`.
31+ ///
32+ /// Zero-sized element types are rejected at compile time when constructing a buffer.
33+ ///
34+ /// ```compile_fail
35+ /// use vortex_buffer::Buffer;
36+ /// let _ = Buffer::<()>::empty();
37+ /// ```
38+ ///
39+ /// ```compile_fail
40+ /// use vortex_buffer::Buffer;
41+ /// let _ = Buffer::from(vec![(); 3]);
42+ /// ```
43+ ///
44+ /// ```compile_fail
45+ /// use vortex_buffer::{Buffer, ByteBuffer};
46+ /// let _ = Buffer::<()>::from_byte_buffer(ByteBuffer::empty());
47+ /// ```
48+ ///
49+ /// ```compile_fail
50+ /// use bytes::Bytes;
51+ /// use vortex_buffer::{Alignment, Buffer};
52+ /// let _ = Buffer::<()>::from_bytes_aligned(Bytes::new(), Alignment::none());
53+ /// ```
3154#[ derive( Clone ) ]
3255pub struct Buffer < T > {
33- /// The first element in this view; may dangle for an empty buffer or zero-sized `T` .
56+ /// The first element in this view; may dangle for an empty buffer.
3457 pub ( crate ) ptr : NonNull < T > ,
3558 /// The number of initialized `T` values visible from `ptr`.
3659 pub ( crate ) length : usize ,
@@ -216,6 +239,7 @@ impl<T> Buffer<T> {
216239 ///
217240 /// This does not allocate. Empty buffers use an aligned dangling pointer.
218241 pub fn empty_aligned ( alignment : Alignment ) -> Self {
242+ const { assert ! ( size_of:: <T >( ) != 0 , "ZSTs are not supported" ) } ;
219243 if !alignment. is_aligned_to ( Alignment :: of :: < T > ( ) ) {
220244 vortex_panic ! (
221245 "Alignment {} must align to the scalar type's alignment {}" ,
@@ -263,12 +287,9 @@ impl<T> Buffer<T> {
263287 /// ## Panics
264288 ///
265289 /// Panics if the buffer is not aligned to the given alignment, if the length is not a multiple
266- /// of the size of `T`, if `T` is zero-sized, or if the given alignment is not aligned to that of `T`.
290+ /// of the size of `T`, or if the given alignment is not aligned to that of `T`.
267291 pub fn from_byte_buffer_aligned ( buffer : ByteBuffer , alignment : Alignment ) -> Self {
268- assert ! (
269- size_of:: <T >( ) != 0 ,
270- "cannot infer a zero-sized element count from bytes"
271- ) ;
292+ const { assert ! ( size_of:: <T >( ) != 0 , "ZSTs are not supported" ) } ;
272293 if !alignment. is_aligned_to ( Alignment :: of :: < T > ( ) ) {
273294 vortex_panic ! (
274295 "Alignment {} must be compatible with the scalar type's alignment {}" ,
@@ -299,12 +320,9 @@ impl<T> Buffer<T> {
299320 /// ## Panics
300321 ///
301322 /// Panics if the buffer is not aligned to the size of `T`, or the length is not a multiple of
302- /// the size of `T`, or if `T` is zero-sized .
323+ /// the size of `T`.
303324 pub fn from_bytes_aligned ( bytes : Bytes , alignment : Alignment ) -> Self {
304- assert ! (
305- size_of:: <T >( ) != 0 ,
306- "cannot infer a zero-sized element count from bytes"
307- ) ;
325+ const { assert ! ( size_of:: <T >( ) != 0 , "ZSTs are not supported" ) } ;
308326 if !alignment. is_aligned_to ( Alignment :: of :: < T > ( ) ) {
309327 vortex_panic ! (
310328 "Alignment {} must be compatible with the scalar type's alignment {}" ,
@@ -555,7 +573,7 @@ impl<T> Buffer<T> {
555573 let subset_end = subset_start
556574 . checked_add ( size_of_val ( subset) )
557575 . vortex_expect ( "slice_ref address overflow" ) ;
558- if subset . len ( ) > self . len ( ) || subset_start < start || subset_end > end {
576+ if subset_start < start || subset_end > end {
559577 vortex_panic ! ( "slice_ref subset must be contained in the buffer" ) ;
560578 }
561579
@@ -627,9 +645,7 @@ impl<T> Buffer<T> {
627645 match Arc :: try_unwrap ( backing) {
628646 Ok ( BufferBacking :: Owned ( allocation) ) => {
629647 let offset = ptr. addr ( ) . get ( ) - allocation. ptr ( ) . addr ( ) . get ( ) ;
630- let capacity = if size_of :: < T > ( ) == 0 {
631- usize:: MAX
632- } else if allocation. size ( ) == 0 {
648+ let capacity = if allocation. size ( ) == 0 {
633649 0
634650 } else {
635651 ( allocation. size ( ) - offset) / size_of :: < T > ( )
@@ -805,10 +821,11 @@ where
805821 T : Send + Sync + ' static ,
806822{
807823 fn from ( value : Vec < T > ) -> Self {
824+ const { assert ! ( size_of:: <T >( ) != 0 , "ZSTs are not supported" ) } ;
808825 let length = value. len ( ) ;
809826 let alignment = Alignment :: of :: < T > ( ) ;
810827 if std:: mem:: needs_drop :: < T > ( ) {
811- // Keep the typed owner so its elements are dropped, including zero-sized elements .
828+ // Keep the typed owner so its elements are dropped.
812829 Self {
813830 ptr : NonNull :: new ( value. as_ptr ( ) . cast_mut ( ) )
814831 . vortex_expect ( "a Vec always has a non-null pointer" ) ,
@@ -882,7 +899,7 @@ fn empty_ptr<T>() -> NonNull<T> {
882899
883900/// Owned iterator over a [`Buffer`].
884901pub struct BufferIterator < T : Copy > {
885- // Keep the buffer alive; its length also counts the remaining zero-sized elements .
902+ // Keep the buffer alive for the duration of the iteration .
886903 _buffer : Buffer < T > ,
887904 ptr : * const T ,
888905 end : * const T ,
@@ -898,11 +915,6 @@ impl<T: Copy> Iterator for BufferIterator<T> {
898915
899916 #[ inline]
900917 fn next ( & mut self ) -> Option < Self :: Item > {
901- if size_of :: < T > ( ) == 0 {
902- let value = self . _buffer . as_slice ( ) . last ( ) . copied ( ) ?;
903- self . _buffer . length -= 1 ;
904- return Some ( value) ;
905- }
906918 if self . ptr == self . end {
907919 None
908920 } else {
@@ -915,12 +927,7 @@ impl<T: Copy> Iterator for BufferIterator<T> {
915927
916928 #[ inline]
917929 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
918- let remaining = if size_of :: < T > ( ) == 0 {
919- self . _buffer . length
920- } else {
921- // SAFETY: both cursors belong to the buffer and T is not zero-sized.
922- ( unsafe { self . end . offset_from ( self . ptr ) } ) as usize
923- } ;
930+ let remaining = unsafe { self . end . offset_from ( self . ptr ) } as usize ;
924931 ( remaining, Some ( remaining) )
925932 }
926933}
0 commit comments