Skip to main content

lsm_tree/slice/slice_bytes/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2024-present, fjall-rs
3// Copyright (c) 2026-present, Structured World Foundation
4
5use bytes::Bytes;
6
7pub use bytes::BytesMut as Builder;
8
9/// An immutable byte slice that can be cloned without additional heap allocation
10///
11/// There is no guarantee of any sort of alignment for zero-copy (de)serialization.
12#[derive(Debug, Clone, Eq, Hash, Ord)]
13pub struct Slice(pub(super) Bytes);
14
15impl Slice {
16    /// Construct a [`Slice`] from a byte slice.
17    #[must_use]
18    pub fn new(bytes: &[u8]) -> Self {
19        Self(Bytes::copy_from_slice(bytes))
20    }
21
22    #[doc(hidden)]
23    #[must_use]
24    pub fn empty() -> Self {
25        Self(Bytes::from_static(&[]))
26    }
27
28    #[doc(hidden)]
29    #[must_use]
30    pub unsafe fn builder_unzeroed(len: usize) -> Builder {
31        // Use `with_capacity` & `set_len`` to avoid zeroing the buffer
32        let mut builder = Builder::with_capacity(len);
33
34        // SAFETY: we just allocated `len` bytes, and `read_exact` will fail if
35        // it doesn't fill the buffer, subsequently dropping the uninitialized
36        // BytesMut object
37        #[expect(unsafe_code, reason = "see safety")]
38        unsafe {
39            builder.set_len(len);
40        }
41
42        builder
43    }
44
45    #[doc(hidden)]
46    #[must_use]
47    pub fn slice(&self, range: impl std::ops::RangeBounds<usize>) -> Self {
48        Self(self.0.slice(range))
49    }
50
51    #[doc(hidden)]
52    #[must_use]
53    pub fn fused(left: &[u8], right: &[u8]) -> Self {
54        use std::io::Write;
55
56        let len = left.len() + right.len();
57        let mut builder = unsafe { Self::builder_unzeroed(len) };
58        {
59            let mut writer = &mut builder[..];
60
61            #[expect(
62                clippy::expect_used,
63                reason = "writing into a pre-allocated buffer of exact size cannot fail"
64            )]
65            writer.write_all(left).expect("should write");
66            #[expect(
67                clippy::expect_used,
68                reason = "writing into a pre-allocated buffer of exact size cannot fail"
69            )]
70            writer.write_all(right).expect("should write");
71        }
72
73        Self(builder.freeze())
74    }
75
76    /// Constructs a [`Slice`] from an I/O reader by pulling in `len` bytes.
77    ///
78    /// The reader may not read the existing buffer.
79    #[doc(hidden)]
80    pub fn from_reader<R: std::io::Read>(reader: &mut R, len: usize) -> std::io::Result<Self> {
81        let mut builder = unsafe { Self::builder_unzeroed(len) };
82
83        // SAFETY: Normally, read_exact over an uninitialized buffer is UB,
84        // however we know that in lsm-tree etc. only I/O readers or cursors over Vecs are used
85        // so it's safe
86        //
87        // The safe API is unstable: https://github.com/rust-lang/rust/issues/78485
88        reader.read_exact(&mut builder)?;
89
90        Ok(Self(builder.freeze()))
91    }
92}
93
94impl From<Bytes> for Slice {
95    fn from(value: Bytes) -> Self {
96        Self(value)
97    }
98}
99
100impl From<Slice> for Bytes {
101    fn from(value: Slice) -> Self {
102        value.0
103    }
104}
105
106// Bytes::from<Vec<u8>> is zero-copy optimized
107impl From<Vec<u8>> for Slice {
108    fn from(value: Vec<u8>) -> Self {
109        Self(Bytes::from(value))
110    }
111}
112
113// Bytes::from<String> is zero-copy optimized
114impl From<String> for Slice {
115    fn from(value: String) -> Self {
116        Self(Bytes::from(value))
117    }
118}