@@ -120,24 +120,6 @@ impl<T> Buffer<T> {
120120 }
121121 }
122122
123- fn from_bytes ( bytes : Bytes , alignment : Alignment ) -> Self {
124- if size_of :: < T > ( ) == 0 {
125- vortex_panic ! ( "cannot infer a zero-sized buffer length from bytes" ) ;
126- }
127- let length = bytes. len ( ) / size_of :: < T > ( ) ;
128- if length == 0 {
129- return Self :: empty_aligned ( alignment) ;
130- }
131- let ptr =
132- NonNull :: new ( bytes. as_ptr ( ) . cast_mut ( ) . cast ( ) ) . vortex_expect ( "Bytes pointer is null" ) ;
133- Self {
134- ptr,
135- length,
136- alignment,
137- backing : Some ( Arc :: new ( BufferBacking :: Bytes ( bytes) ) ) ,
138- }
139- }
140-
141123 #[ cfg( feature = "arrow" ) ]
142124 pub ( crate ) fn from_arrow_owner (
143125 arrow : arrow_buffer:: Buffer ,
@@ -311,40 +293,6 @@ impl<T> Buffer<T> {
311293 }
312294 }
313295
314- /// Create a `Buffer<T>` zero-copy from a `Bytes`.
315- ///
316- /// ## Panics
317- ///
318- /// Panics if the buffer is not aligned to the size of `T`, or the length is not a multiple of
319- /// the size of `T`. Also panics if `T` is zero-sized because bytes do not contain an element
320- /// count.
321- pub fn from_bytes_aligned ( bytes : Bytes , alignment : Alignment ) -> Self {
322- if size_of :: < T > ( ) == 0 {
323- vortex_panic ! ( "cannot infer a zero-sized buffer length from bytes" ) ;
324- }
325- if !alignment. is_aligned_to ( Alignment :: of :: < T > ( ) ) {
326- vortex_panic ! (
327- "Alignment {} must be compatible with the scalar type's alignment {}" ,
328- alignment,
329- Alignment :: of:: <T >( ) ,
330- ) ;
331- }
332- if !alignment. is_ptr_aligned ( bytes. as_ptr ( ) ) {
333- vortex_panic ! (
334- "Bytes alignment must align to the requested alignment {}" ,
335- alignment,
336- ) ;
337- }
338- if !bytes. len ( ) . is_multiple_of ( size_of :: < T > ( ) ) {
339- vortex_panic ! (
340- "Bytes length {} must be a multiple of the scalar type's size {}" ,
341- bytes. len( ) ,
342- size_of:: <T >( )
343- ) ;
344- }
345- Self :: from_bytes ( bytes, alignment)
346- }
347-
348296 /// Create a buffer with values from the TrustedLen iterator.
349297 /// Should be preferred over `from_iter` when the iterator is known to be `TrustedLen`.
350298 pub fn from_trusted_len_iter < I : TrustedLen < Item = T > > ( iter : I ) -> Self {
@@ -715,6 +663,36 @@ impl<T> Buffer<T> {
715663 }
716664}
717665
666+ impl Buffer < u8 > {
667+ fn from_bytes ( bytes : Bytes , alignment : Alignment ) -> Self {
668+ if bytes. is_empty ( ) {
669+ return Self :: empty_aligned ( alignment) ;
670+ }
671+ let ptr = NonNull :: new ( bytes. as_ptr ( ) . cast_mut ( ) ) . vortex_expect ( "Bytes pointer is null" ) ;
672+ Self {
673+ ptr,
674+ length : bytes. len ( ) ,
675+ alignment,
676+ backing : Some ( Arc :: new ( BufferBacking :: Bytes ( bytes) ) ) ,
677+ }
678+ }
679+
680+ /// Create a byte buffer zero-copy from [`Bytes`] with the requested alignment.
681+ ///
682+ /// ## Panics
683+ ///
684+ /// Panics if `bytes` is not aligned to `alignment`.
685+ pub fn from_bytes_aligned ( bytes : Bytes , alignment : Alignment ) -> Self {
686+ if !alignment. is_ptr_aligned ( bytes. as_ptr ( ) ) {
687+ vortex_panic ! (
688+ "Bytes alignment must align to the requested alignment {}" ,
689+ alignment,
690+ ) ;
691+ }
692+ Self :: from_bytes ( bytes, alignment)
693+ }
694+ }
695+
718696impl < T > Buffer < T > {
719697 /// Transmute a `Buffer<T>` into a `Buffer<U>`.
720698 ///
@@ -1298,15 +1276,6 @@ mod test {
12981276 drop ( Buffer :: < ( ) > :: from_byte_buffer ( ByteBuffer :: empty ( ) ) ) ;
12991277 }
13001278
1301- #[ test]
1302- #[ should_panic( expected = "zero-sized" ) ]
1303- fn zero_sized_from_bytes_is_rejected ( ) {
1304- drop ( Buffer :: < ( ) > :: from_bytes_aligned (
1305- Bytes :: new ( ) ,
1306- Alignment :: of :: < ( ) > ( ) ,
1307- ) ) ;
1308- }
1309-
13101279 #[ test]
13111280 fn empty_aligned_max_alignment ( ) {
13121281 // Empty buffers are backed by a static and must satisfy any valid alignment.
0 commit comments