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