Skip to main content

byteview/
lib.rs

1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5//! An immutable byte slice that may be inlined, and can be partially cloned without heap allocation.
6//!
7//! The length is limited to 2^32 bytes (4 GiB).
8//!
9//! ```
10//! # use byteview::ByteView;
11//! let slice = ByteView::from("helloworld_thisisaverylongstring");
12//!
13//! // No heap allocation - increases the ref count like an Arc<[u8]>
14//! let full_copy = slice.clone();
15//! drop(full_copy);
16//!
17//! // No heap allocation - increases the ref count like an Arc<[u8]>, but we only get a subslice
18//! let copy = slice.slice(11..);
19//! assert_eq!(b"thisisaverylongstring", &*copy);
20//!
21//! // No heap allocation - if the slice is small enough, it will be inlined into the struct...
22//! let copycopy = copy.slice(0..4);
23//! assert_eq!(b"this", &*copycopy);
24//!
25//! // ...so no ref count incrementing is done
26//! assert_eq!(2, slice.ref_count());
27//!
28//! drop(copy);
29//! assert_eq!(1, slice.ref_count());
30//!
31//! drop(copycopy);
32//! assert_eq!(1, slice.ref_count());
33//!
34//! // Our original slice will be automatically freed if all slices vanish
35//! drop(slice);
36//! ```
37
38#![deny(clippy::all, missing_docs, clippy::cargo)]
39#![allow(
40    clippy::cargo_common_metadata,
41    reason = "not every internal workspace package is independently published"
42)]
43#![deny(clippy::unwrap_used)]
44#![deny(clippy::indexing_slicing)]
45#![warn(
46    clippy::pedantic,
47    clippy::nursery,
48    clippy::expect_used,
49    clippy::unwrap_used,
50    clippy::indexing_slicing,
51    clippy::needless_lifetimes
52)]
53
54mod builder;
55mod byteview;
56mod strview;
57
58pub use {byteview::ByteView, strview::StrView};
59
60#[doc(hidden)]
61pub use byteview::{Builder, Mutator};