about summary refs log tree commit diff
path: root/tvix/tools/weave/src/bytes.rs
blob: c6dc2ebb4492defb0d1da18ae7828fc375b54f3c (plain) (blame)
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
use owning_ref::{OwningRef, StableAddress};
use polars::export::arrow::buffer::Buffer;
use std::ops::Deref;

/// An shared `[[u8; N]]` backed by a Polars [Buffer].
pub type FixedBytes<const N: usize> = OwningRef<Bytes, [[u8; N]]>;

/// Wrapper struct to make [Buffer] implement [StableAddress].
/// TODO(edef): upstream the `impl`
pub struct Bytes(pub Buffer<u8>);

/// SAFETY: [Buffer] is always an Arc+Vec indirection.
unsafe impl StableAddress for Bytes {}

impl Bytes {
    pub fn map<U: ?Sized>(self, f: impl FnOnce(&[u8]) -> &U) -> OwningRef<Self, U> {
        OwningRef::new(self).map(f)
    }
}

impl Deref for Bytes {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}