Skip to content

Commit 54f2cfc

Browse files
committed
builder API
1 parent 513a6a7 commit 54f2cfc

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
name = "byteview"
33
description = "Thin, immutable zero-copy slice type"
44
license = "MIT OR Apache-2.0"
5-
version = "0.7.0"
5+
version = "0.8.0"
66
edition = "2021"
7-
rust-version = "1.74"
7+
rust-version = "1.81"
88
readme = "README.md"
99
include = ["src/**/*", "LICENSE-APACHE", "LICENSE-MIT", "README.md"]
1010
repository = "https://github.com/fjall-rs/byteview"
@@ -28,9 +28,6 @@ rand = "0.9.0"
2828
# TODO: Need MSRV 1.81
2929
# rkyv = { version = "0.8.10", features = ["bytecheck", "unaligned"] }
3030

31-
# half 2.5.0 has MSRV 1.81
32-
half = "=2.4.0"
33-
3431
[[bench]]
3532
name = "bench"
3633
harness = false

src/builder.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use crate::ByteView;
2+
3+
/// A builder for a [`ByteView`] that allows mutation before freezing it.
4+
pub struct Builder(ByteView);
5+
6+
impl Builder {
7+
/// Creates a new builder.
8+
#[must_use]
9+
pub const fn new(inner: ByteView) -> Self {
10+
Self(inner)
11+
}
12+
13+
/// Converts the builder into a [`ByteView`], making it immutable.
14+
#[must_use]
15+
pub fn freeze(mut self) -> ByteView {
16+
self.0.update_prefix();
17+
self.0
18+
}
19+
}
20+
21+
impl std::ops::Deref for Builder {
22+
type Target = [u8];
23+
24+
fn deref(&self) -> &Self::Target {
25+
&self.0
26+
}
27+
}
28+
29+
impl std::ops::DerefMut for Builder {
30+
fn deref_mut(&mut self) -> &mut Self::Target {
31+
self.0.get_mut_slice()
32+
}
33+
}

src/byteview.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::{
1212
},
1313
};
1414

15+
pub use crate::builder::Builder;
16+
1517
#[cfg(target_pointer_width = "64")]
1618
const INLINE_SIZE: usize = 20;
1719

@@ -207,6 +209,12 @@ impl Drop for Mutator<'_> {
207209
}
208210

209211
impl ByteView {
212+
#[doc(hidden)]
213+
#[must_use]
214+
pub fn builder_unzeroed(len: usize) -> Builder {
215+
Builder::new(Self::with_size_unzeroed(len))
216+
}
217+
210218
fn prefix(&self) -> &[u8] {
211219
let len = PREFIX_SIZE.min(self.len());
212220

@@ -218,7 +226,7 @@ impl ByteView {
218226
self.len() <= INLINE_SIZE
219227
}
220228

221-
fn update_prefix(&mut self) {
229+
pub(crate) fn update_prefix(&mut self) {
222230
if !self.is_inline() {
223231
unsafe {
224232
let slice_ptr: &[u8] = &*self;
@@ -639,7 +647,7 @@ impl ByteView {
639647
unsafe { self.trailer.short.len as usize }
640648
}
641649

642-
fn get_mut_slice(&mut self) -> &mut [u8] {
650+
pub(crate) fn get_mut_slice(&mut self) -> &mut [u8] {
643651
let len = self.len();
644652

645653
if self.is_inline() {

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@
4747
clippy::needless_lifetimes
4848
)]
4949

50+
mod builder;
5051
mod byteview;
5152
mod strview;
5253

5354
pub use {byteview::ByteView, strview::StrView};
5455

5556
#[doc(hidden)]
56-
pub use byteview::Mutator;
57+
pub use byteview::{Builder, Mutator};

0 commit comments

Comments
 (0)