Skip to main content

lsm_tree/slice/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2024-present, fjall-rs
3// Copyright (c) 2026-present, Dmitry Prudnikov
4
5// Using tokio bytes
6#[cfg(feature = "bytes_1")]
7mod slice_bytes;
8
9// Using byteview
10#[cfg(not(feature = "bytes_1"))]
11mod slice_default;
12
13mod key_chunks;
14
15pub(crate) use key_chunks::KeyChunks;
16
17use alloc::sync::Arc;
18#[cfg(not(feature = "std"))]
19use alloc::{string::String, vec::Vec};
20
21use crate::path::{Path, PathBuf};
22
23#[cfg(not(feature = "bytes_1"))]
24pub(crate) use slice_default::UnfilledSlice;
25#[cfg(not(feature = "bytes_1"))]
26pub use slice_default::{Builder, Slice};
27
28#[cfg(feature = "bytes_1")]
29pub(crate) use slice_bytes::UnfilledSlice;
30#[cfg(feature = "bytes_1")]
31pub use slice_bytes::{Builder, Slice};
32
33impl AsRef<[u8]> for Slice {
34    fn as_ref(&self) -> &[u8] {
35        &self.0
36    }
37}
38
39impl From<&[u8]> for Slice {
40    fn from(value: &[u8]) -> Self {
41        #[cfg(not(feature = "bytes_1"))]
42        {
43            Self(crate::byteview::ByteView::new(value))
44        }
45
46        #[cfg(feature = "bytes_1")]
47        {
48            Self(bytes::Bytes::from(value.to_vec()))
49        }
50    }
51}
52
53impl From<Arc<[u8]>> for Slice {
54    fn from(value: Arc<[u8]>) -> Self {
55        Self::from(&*value)
56    }
57}
58
59impl From<&Vec<u8>> for Slice {
60    fn from(value: &Vec<u8>) -> Self {
61        Self::from(value.as_slice())
62    }
63}
64
65impl From<&str> for Slice {
66    fn from(value: &str) -> Self {
67        Self::from(value.as_bytes())
68    }
69}
70
71impl From<&String> for Slice {
72    fn from(value: &String) -> Self {
73        Self::from(value.as_str())
74    }
75}
76
77impl From<&Path> for Slice {
78    fn from(value: &Path) -> Self {
79        // std: `OsStr::as_encoded_bytes`. no_std: the key is UTF-8, so
80        // `as_os_str()` is a `&str` and `as_bytes()` yields the same bytes.
81        #[cfg(feature = "std")]
82        {
83            Self::from(value.as_os_str().as_encoded_bytes())
84        }
85        #[cfg(not(feature = "std"))]
86        {
87            Self::from(value.as_os_str().as_bytes())
88        }
89    }
90}
91
92impl From<PathBuf> for Slice {
93    fn from(value: PathBuf) -> Self {
94        #[cfg(feature = "std")]
95        {
96            Self::from(value.as_os_str().as_encoded_bytes())
97        }
98        #[cfg(not(feature = "std"))]
99        {
100            Self::from(value.as_os_str().as_bytes())
101        }
102    }
103}
104
105impl From<Arc<str>> for Slice {
106    fn from(value: Arc<str>) -> Self {
107        Self::from(&*value)
108    }
109}
110
111impl<const N: usize> From<[u8; N]> for Slice {
112    fn from(value: [u8; N]) -> Self {
113        Self::from(value.as_slice())
114    }
115}
116
117impl<const N: usize> From<&[u8; N]> for Slice {
118    fn from(value: &[u8; N]) -> Self {
119        Self::from(value.as_slice())
120    }
121}
122
123impl FromIterator<u8> for Slice {
124    fn from_iter<T>(iter: T) -> Self
125    where
126        T: IntoIterator<Item = u8>,
127    {
128        Vec::from_iter(iter).into()
129    }
130}
131
132impl core::ops::Deref for Slice {
133    type Target = [u8];
134
135    fn deref(&self) -> &Self::Target {
136        self.as_ref()
137    }
138}
139
140impl core::borrow::Borrow<[u8]> for Slice {
141    fn borrow(&self) -> &[u8] {
142        self
143    }
144}
145
146impl<T> PartialEq<T> for Slice
147where
148    T: AsRef<[u8]>,
149{
150    fn eq(&self, other: &T) -> bool {
151        self.as_ref() == other.as_ref()
152    }
153}
154
155impl PartialEq<Slice> for &[u8] {
156    fn eq(&self, other: &Slice) -> bool {
157        *self == other.as_ref()
158    }
159}
160
161impl<T> PartialOrd<T> for Slice
162where
163    T: AsRef<[u8]>,
164{
165    fn partial_cmp(&self, other: &T) -> Option<core::cmp::Ordering> {
166        self.as_ref().partial_cmp(other.as_ref())
167    }
168}
169
170impl PartialOrd<Slice> for &[u8] {
171    fn partial_cmp(&self, other: &Slice) -> Option<core::cmp::Ordering> {
172        (*self).partial_cmp(other.as_ref())
173    }
174}
175
176#[cfg(test)]
177#[expect(clippy::expect_used)]
178mod tests;