-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from carlsverre/main
Add `bytes` feature flag
- Loading branch information
Showing
4 changed files
with
213 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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 std::sync::Arc; | ||
|
||
/// An immutable byte slice that can be cloned without additional heap allocation | ||
#[derive(Debug, Clone, Eq, Hash, Ord)] | ||
pub struct Slice(pub(super) Arc<[u8]>); | ||
|
||
impl Slice { | ||
/// Construct a [`Slice`] from a byte slice. | ||
#[must_use] | ||
pub fn new(bytes: &[u8]) -> Self { | ||
Self(bytes.into()) | ||
} | ||
|
||
#[must_use] | ||
#[doc(hidden)] | ||
pub fn with_size(len: usize) -> Self { | ||
// TODO: optimize this with byteview to remove the reallocation | ||
let v = vec![0; len]; | ||
Self(v.into()) | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn from_reader<R: std::io::Read>(reader: &mut R, len: usize) -> std::io::Result<Self> { | ||
let mut view = Self::with_size(len); | ||
let builder = Arc::get_mut(&mut view.0).expect("we are the owner"); | ||
reader.read_exact(builder)?; | ||
Ok(view) | ||
} | ||
} | ||
|
||
// Arc::from<Vec<T>> is specialized | ||
impl From<Vec<u8>> for Slice { | ||
fn from(value: Vec<u8>) -> Self { | ||
Self(Arc::from(value)) | ||
} | ||
} | ||
|
||
// Arc::from<Vec<T>> is specialized | ||
impl From<String> for Slice { | ||
fn from(value: String) -> Self { | ||
Self(Arc::from(value.into_bytes())) | ||
} | ||
} | ||
|
||
// direct conversion | ||
impl From<Arc<[u8]>> for Slice { | ||
fn from(value: Arc<[u8]>) -> Self { | ||
Self(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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 std::sync::Arc; | ||
|
||
use bytes::{Bytes, BytesMut}; | ||
|
||
/// An immutable byte slice that can be cloned without additional heap allocation | ||
#[derive(Debug, Clone, Eq, Hash, Ord)] | ||
pub struct Slice(pub(super) Bytes); | ||
|
||
impl Slice { | ||
/// Construct a [`Slice`] from a byte slice. | ||
#[must_use] | ||
pub fn new(bytes: &[u8]) -> Self { | ||
Self(Bytes::copy_from_slice(bytes)) | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn from_reader<R: std::io::Read>(reader: &mut R, len: usize) -> std::io::Result<Self> { | ||
let mut builder = BytesMut::zeroed(len); | ||
reader.read_exact(&mut builder)?; | ||
Ok(builder.freeze().into()) | ||
} | ||
} | ||
|
||
impl From<Bytes> for Slice { | ||
fn from(value: Bytes) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl From<Slice> for Bytes { | ||
fn from(value: Slice) -> Self { | ||
value.0 | ||
} | ||
} | ||
|
||
// Bytes::from<Vec<u8>> is zero-copy optimized | ||
impl From<Vec<u8>> for Slice { | ||
fn from(value: Vec<u8>) -> Self { | ||
Self(Bytes::from(value)) | ||
} | ||
} | ||
|
||
// Bytes::from<String> is zero-copy optimized | ||
impl From<String> for Slice { | ||
fn from(value: String) -> Self { | ||
Self(Bytes::from(value)) | ||
} | ||
} | ||
|
||
// Needed because slice_arc specializes this impl | ||
impl From<Arc<[u8]>> for Slice { | ||
fn from(value: Arc<[u8]>) -> Self { | ||
Self::new(value.as_ref()) | ||
} | ||
} |