value_log/slice/slice_default.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
// Copyright (c) 2024-present, fjall-rs
// This source code is licensed under both the Apache 2.0 and MIT License
// (found in the LICENSE-* files in the repository)
use byteview::ByteView;
/// An immutable byte slice that can be cloned without additional heap allocation
///
/// There is no guarantee of any sort of alignment for zero-copy (de)serialization.
#[derive(Debug, Clone, Eq, Hash, Ord)]
pub struct Slice(pub(super) ByteView);
impl Slice {
/// Construct a [`Slice`] from a byte slice.
#[must_use]
pub fn new(bytes: &[u8]) -> Self {
Self(bytes.into())
}
#[doc(hidden)]
#[must_use]
pub fn empty() -> Self {
Self(ByteView::new(&[]))
}
#[doc(hidden)]
#[must_use]
pub fn slice(&self, range: impl std::ops::RangeBounds<usize>) -> Self {
Self(self.0.slice(range))
}
#[must_use]
#[doc(hidden)]
pub fn with_size(len: usize) -> Self {
Self(ByteView::with_size(len))
}
/// Constructs a [`Slice`] from an I/O reader by pulling in `len` bytes.
#[doc(hidden)]
pub fn from_reader<R: std::io::Read>(reader: &mut R, len: usize) -> std::io::Result<Self> {
let view = ByteView::from_reader(reader, len)?;
Ok(Self(view))
}
}
// Arc::from<Vec<u8>> is specialized
impl From<Vec<u8>> for Slice {
fn from(value: Vec<u8>) -> Self {
Self(ByteView::from(value))
}
}
// Arc::from<Vec<String>> is specialized
impl From<String> for Slice {
fn from(value: String) -> Self {
Self(ByteView::from(value.into_bytes()))
}
}