narrow/
buffer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Traits for memory buffers.

use crate::{FixedSize, Index, Length};
use std::{marker::PhantomData, mem, rc::Rc, slice, sync::Arc};

/// A memory buffer type constructor for Arrow data.
///
/// The generic associated type constructor [`Self::Buffer`] defines the
/// [`Buffer`] type that stores [`FixedSize`] items.
///
// note
// Arrow buffers are like Rust slices with "primitive" item types.
// Another way to implement the buffer trait: a subtrait of Borrow<[T]> and then
// implement Buffer<T> for all U: Borrow<[T] where T: FixedSize, however,the approach here is a little
// bit more elaborate to also support buffer types that don't implement Borrow<[T]>.
pub trait BufferType {
    /// A [`Buffer`] type for [`FixedSize`] items of type `T`.
    type Buffer<T: FixedSize>: Buffer<T>;
}

/// An immutable reference to a buffer.
///
/// This can be used to provide immutable access to an internal buffer.
pub trait BufferRef<T: FixedSize> {
    /// The [Buffer] type.
    type Buffer: Buffer<T>;

    /// Returns an immutable reference to a buffer.
    fn buffer_ref(&self) -> &Self::Buffer;
}

/// A mutable reference to a buffer.
///
/// This can be used to provide mutable access to an internal buffer.
pub trait BufferRefMut<T: FixedSize> {
    /// The [`BufferMut`] type.
    type BufferMut: BufferMut<T>;

    /// Returns a mutable reference to a buffer.
    fn buffer_ref_mut(&mut self) -> &mut Self::BufferMut;
}

/// A contiguous immutable memory buffer for Arrow data.
pub trait Buffer<T: FixedSize>: Index + Length {
    /// Extracts a slice containing the entire buffer.
    fn as_slice(&self) -> &[T];

    /// Returns the contents of the entire buffer as a byte slice.
    fn as_bytes(&self) -> &[u8] {
        // Safety:
        // - The pointer returned by slice::as_ptr (via Borrow) points to slice::len()
        //   consecutive properly initialized values of type T, with size_of::<T> bytes
        //   per element.
        unsafe {
            slice::from_raw_parts(
                self.as_slice().as_ptr().cast(),
                mem::size_of_val(self.as_slice()),
            )
        }
    }
}

/// A contiguous mutable memory buffer for Arrow data.
pub trait BufferMut<T: FixedSize>: Buffer<T> {
    /// Extracts a mutable slice containing the entire buffer.
    fn as_mut_slice(&mut self) -> &mut [T];

    /// Returns the contents of the entire buffer as a mutable byte slice.
    fn as_mut_bytes(&mut self) -> &mut [u8] {
        // Safety:
        // - The pointer returned by slice::as_mut_ptr (via Borrow) points to slice::len()
        //   consecutive properly initialized values of type T, with size_of::<T> bytes
        //   per element.
        unsafe {
            slice::from_raw_parts_mut(
                self.as_mut_slice().as_mut_ptr().cast(),
                mem::size_of_val(self.as_slice()),
            )
        }
    }
}

/// A [`BufferType`] for a single item.
#[derive(Clone, Copy, Debug)]
pub struct SingleBuffer;

impl BufferType for SingleBuffer {
    type Buffer<T: FixedSize> = <ArrayBuffer<1> as BufferType>::Buffer<T>;
}

/// A [`BufferType`] implementation for array.
///
/// Stores items `T` in `[T; N]`.
#[derive(Clone, Copy, Debug)]
pub struct ArrayBuffer<const N: usize>;

impl<const N: usize> BufferType for ArrayBuffer<N> {
    type Buffer<T: FixedSize> = [T; N];
}

impl<T: FixedSize, const N: usize> Buffer<T> for [T; N] {
    fn as_slice(&self) -> &[T] {
        self.as_slice()
    }
}

impl<T: FixedSize, const N: usize> BufferMut<T> for [T; N] {
    fn as_mut_slice(&mut self) -> &mut [T] {
        self.as_mut_slice()
    }
}

/// A [`BufferType`] implementation for array in array.
///
/// Stores items `T` in `[[T; M]; N]`.
#[derive(Clone, Copy, Debug)]
pub struct ArrayArrayBuffer<const M: usize, const N: usize>;

impl<const M: usize, const N: usize> BufferType for ArrayArrayBuffer<M, N> {
    type Buffer<T: FixedSize> = [[T; M]; N];
}

impl<T: FixedSize, const M: usize, const N: usize> Buffer<T> for [[T; M]; N] {
    fn as_slice(&self) -> &[T] {
        // self.flatten() is nightly
        // SAFETY: `[T]` is layout-identical to `[[T; M]; N]`
        unsafe { std::slice::from_raw_parts(self.as_ptr().cast(), M * N) }
    }
}

impl<T: FixedSize, const M: usize, const N: usize> BufferMut<T> for [[T; M]; N] {
    fn as_mut_slice(&mut self) -> &mut [T] {
        // self.flatten() is nightly
        // SAFETY: `[T]` is layout-identical to `[[T; M]; N]`
        unsafe { std::slice::from_raw_parts_mut(self.as_mut_ptr().cast(), M * N) }
    }
}

/// A [`BufferType`] implementation for slice.
///
/// Stores items `T` in `&[T]`.
#[derive(Clone, Copy, Debug)]
pub struct SliceBuffer<'a>(PhantomData<&'a ()>);

impl<'a> BufferType for SliceBuffer<'a> {
    type Buffer<T: FixedSize> = &'a [T];
}

impl<T: FixedSize> Buffer<T> for &[T] {
    fn as_slice(&self) -> &[T] {
        self
    }
}

/// A [`BufferType`] implementation for mutable slice.
///
/// Stores items `T` in `&mut [T]`.
#[derive(Clone, Copy, Debug)]
pub struct SliceMutBuffer<'a>(PhantomData<&'a ()>);

impl<'a> BufferType for SliceMutBuffer<'a> {
    type Buffer<T: FixedSize> = &'a mut [T];
}

impl<T: FixedSize> Buffer<T> for &mut [T] {
    fn as_slice(&self) -> &[T] {
        self
    }
}

impl<T: FixedSize> BufferMut<T> for &mut [T] {
    fn as_mut_slice(&mut self) -> &mut [T] {
        self
    }
}

/// A [`BufferType`] implementation for slice with array items.
///
/// Stores items `T` in `&[[T; N]]`.
#[derive(Clone, Copy, Debug)]
pub struct SliceArrayBuffer<'a, const N: usize>(PhantomData<&'a ()>);

impl<'a, const N: usize> BufferType for SliceArrayBuffer<'a, N> {
    type Buffer<T: FixedSize> = &'a [[T; N]];
}

impl<T: FixedSize, const N: usize> Buffer<T> for &[[T; N]] {
    fn as_slice(&self) -> &[T] {
        // self.flatten() is nightly
        // SAFETY: `[T]` is layout-identical to `[T; N]`
        unsafe { std::slice::from_raw_parts(self.as_ptr().cast(), <[[T; N]]>::len(self) * N) }
    }
}

/// A [`BufferType`] implementation for mutable slice with array items.
///
/// Stores items `T` in `&mut [[T; N]]`.
#[derive(Clone, Copy, Debug)]
pub struct SliceArrayMutBuffer<'a, const N: usize>(PhantomData<&'a ()>);

impl<'a, const N: usize> BufferType for SliceArrayMutBuffer<'a, N> {
    type Buffer<T: FixedSize> = &'a mut [[T; N]];
}

impl<T: FixedSize, const N: usize> Buffer<T> for &mut [[T; N]] {
    fn as_slice(&self) -> &[T] {
        // self.flatten() is nightly
        // SAFETY: `[T]` is layout-identical to `[T; N]`
        unsafe { std::slice::from_raw_parts(self.as_ptr().cast(), <[[T; N]]>::len(self) * N) }
    }
}

impl<T: FixedSize, const N: usize> BufferMut<T> for &mut [[T; N]] {
    fn as_mut_slice(&mut self) -> &mut [T] {
        // self.flatten() is nightly
        // SAFETY: `[T]` is layout-identical to `[T; N]`
        unsafe {
            std::slice::from_raw_parts_mut(self.as_mut_ptr().cast(), <[[T; N]]>::len(self) * N)
        }
    }
}

/// A [`BufferType`] implementation for [`Vec`].
///
/// Stores items `T` in `Vec<T>`.
#[derive(Clone, Copy, Debug)]
pub struct VecBuffer;

impl BufferType for VecBuffer {
    type Buffer<T: FixedSize> = Vec<T>;
}

impl<T: FixedSize> Buffer<T> for Vec<T> {
    fn as_slice(&self) -> &[T] {
        self.as_slice()
    }
}

impl<T: FixedSize> BufferMut<T> for Vec<T> {
    fn as_mut_slice(&mut self) -> &mut [T] {
        self.as_mut_slice()
    }
}

/// A [`BufferType`] implementation for [`Vec`] with array items.
///
/// Stores items `T` in `Vec<[T;N]>`.
#[derive(Clone, Copy, Debug)]
pub struct VecArrayBuffer<const N: usize>;

impl<const N: usize> BufferType for VecArrayBuffer<N> {
    type Buffer<T: FixedSize> = Vec<[T; N]>;
}

impl<T: FixedSize, const N: usize> Buffer<T> for Vec<[T; N]> {
    fn as_slice(&self) -> &[T] {
        // self.flatten() is nightly
        // SAFETY: `[T]` is layout-identical to `[T; N]`
        unsafe { std::slice::from_raw_parts(self.as_ptr().cast(), Vec::<[T; N]>::len(self) * N) }
    }
}

impl<T: FixedSize, const N: usize> BufferMut<T> for Vec<[T; N]> {
    fn as_mut_slice(&mut self) -> &mut [T] {
        // self.flatten() is nightly
        // SAFETY: `[T]` is layout-identical to `[T; N]`
        unsafe {
            std::slice::from_raw_parts_mut(self.as_mut_ptr().cast(), Vec::<[T; N]>::len(self) * N)
        }
    }
}

/// A [`BufferType`] implementation for [`Box`].
///
/// Stores items `T` in `Box<[T]>`.
#[derive(Clone, Copy, Debug)]
pub struct BoxBuffer;

impl BufferType for BoxBuffer {
    type Buffer<T: FixedSize> = Box<[T]>;
}

impl<T: FixedSize> Buffer<T> for Box<[T]> {
    fn as_slice(&self) -> &[T] {
        <&[T]>::from(self)
    }
}

impl<T: FixedSize> BufferMut<T> for Box<[T]> {
    fn as_mut_slice(&mut self) -> &mut [T] {
        <&mut [T]>::from(self)
    }
}

/// A [`BufferType`] implementation for [`Arc`].
///
/// Stores items `T` in `Arc<[T]>`.
#[derive(Clone, Copy, Debug)]
pub struct ArcBuffer;

impl BufferType for ArcBuffer {
    type Buffer<T: FixedSize> = Arc<[T]>;
}

impl<T: FixedSize> Buffer<T> for Arc<[T]> {
    fn as_slice(&self) -> &[T] {
        <&[T]>::from(self)
    }
}

impl<T: FixedSize> BufferMut<T> for Arc<[T]> {
    fn as_mut_slice(&mut self) -> &mut [T] {
        match Arc::get_mut(self) {
            Some(slice) => slice,
            None => panic!("not safe to mutate shared value"),
        }
    }
}

/// A [`BufferType`] implementation for [`Rc`].
///
/// Stores items `T` in `Rc<[T]>`.
#[derive(Clone, Copy, Debug)]
pub struct RcBuffer;

impl BufferType for RcBuffer {
    type Buffer<T: FixedSize> = Rc<[T]>;
}

impl<T: FixedSize> Buffer<T> for Rc<[T]> {
    fn as_slice(&self) -> &[T] {
        <&[T]>::from(self)
    }
}

impl<T: FixedSize> BufferMut<T> for Rc<[T]> {
    fn as_mut_slice(&mut self) -> &mut [T] {
        match Rc::get_mut(self) {
            Some(slice) => slice,
            None => panic!("not safe to mutate shared value"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn single() {
        let mut single: <SingleBuffer as BufferType>::Buffer<u16> = [1234];
        assert_eq!(single.as_bytes(), [210, 4]);
        single.as_mut_bytes()[1] = 0;
        assert_eq!(single.as_bytes(), [210, 0]);
        single.as_mut_slice()[0] = 1234;
        assert_eq!(single, [1234]);
    }

    #[test]
    fn array() {
        let mut array: <ArrayBuffer<4> as BufferType>::Buffer<u16> = [1, 2, 3, 4];
        assert_eq!(
            <_ as Buffer<u16>>::as_bytes(&array),
            &[1, 0, 2, 0, 3, 0, 4, 0]
        );
        <_ as BufferMut<u16>>::as_mut_bytes(&mut array)[1] = 1;
        assert_eq!(<_ as Buffer<u16>>::as_bytes(&array)[..2], [1, 1]);
        array.as_mut_slice()[0] = 1;
        assert_eq!(array, [1, 2, 3, 4]);
    }

    #[test]
    fn array_array() {
        let mut array_array: <ArrayArrayBuffer<2, 4> as BufferType>::Buffer<u8> =
            [[1, 2], [3, 4], [1, 2], [3, 4]];
        assert_eq!(
            <_ as Buffer<u8>>::as_bytes(&array_array),
            &[1, 2, 3, 4, 1, 2, 3, 4]
        );
        <_ as BufferMut<u8>>::as_mut_bytes(&mut array_array)[1] = 1;
        assert_eq!(
            <_ as Buffer<u8>>::as_slice(&array_array),
            [1, 1, 3, 4, 1, 2, 3, 4]
        );
    }

    #[test]
    fn slice() {
        let slice: <SliceBuffer as BufferType>::Buffer<u16> = &[1234, 4321];
        assert_eq!(slice.as_bytes(), &[210, 4, 225, 16]);
        let mut slice_mut: <SliceMutBuffer as BufferType>::Buffer<u16> = &mut [4321, 1234];
        slice_mut.as_mut_slice()[0] = 1234;
        slice_mut.as_mut_slice()[1] = 4321;
        assert_eq!(slice, slice_mut);
    }

    #[test]
    fn slice_array() {
        let slice_array: <SliceArrayBuffer<2> as BufferType>::Buffer<u32> = &[[1, 2], [3, 4]];
        assert_eq!(<_ as Buffer<u32>>::as_slice(&slice_array), [1, 2, 3, 4]);
        let mut slice_array_mut: <SliceArrayMutBuffer<3> as BufferType>::Buffer<u8> =
            &mut [[1, 2, 3], [4, 5, 6]];
        slice_array_mut.as_mut_slice()[0] = 0;
        assert_eq!(
            <_ as Buffer<u8>>::as_bytes(&slice_array_mut),
            &[0, 2, 3, 4, 5, 6]
        );
    }
}