Expand description
A utility library for working with shared slices of memory.
This crate provides efficient shared buffer implementations ArcSlice and ArcSliceMut.
§Examples
use arc_slice::{ArcSlice, ArcSliceMut};
let mut bytes_mut: ArcSliceMut<[u8]> = ArcSliceMut::new();
bytes_mut.extend_from_slice(b"Hello world");
let mut bytes: ArcSlice<[u8]> = bytes_mut.freeze();
let a: ArcSlice<[u8]> = bytes.subslice(0..5);
assert_eq!(a, b"Hello");
let b: ArcSlice<[u8]> = bytes.split_to(6);
assert_eq!(bytes, b"world");
assert_eq!(b, b"Hello ");Depending on its layout, ArcSlice can also support arbitrary buffers, e.g. shared memory,
and provides optional metadata that can be attached to the buffer.
use std::{
fs::File,
path::{Path, PathBuf},
};
use arc_slice::{buffer::AsRefBuffer, layout::ArcLayout, ArcBytes};
use memmap2::Mmap;
let path = Path::new("README.md").to_owned();
let file = File::open(&path)?;
let mmap = unsafe { Mmap::map(&file)? };
let buffer = AsRefBuffer(mmap);
let bytes: ArcBytes<ArcLayout<true>> = ArcBytes::from_buffer_with_metadata(buffer, path);
assert!(bytes.starts_with(b"# arc-slice"));
assert_eq!(bytes.metadata::<PathBuf>().unwrap(), Path::new("README.md"));§Features
The crate provides the following optional features:
abort-on-refcount-overflow(default): abort on refcount overflow; when disabled, the refcount saturates on overflow, leaking allocated memory (as in Linux kernel refcounting).bstr: implement slice traits forbstrcrate, allowing usage ofArcSlice<BStr>.bytemuck: usebytemuck::Zeroableas a bound for zero-initialization withArcSliceMut::zeroed.bytes: implementBufandBufMuttraits forArcSliceandArcSliceMut.inlined: enable Small String Optimization forArcSliceviainlined::SmallArcSlice.oom-handling(default): enable global out-of-memory handling with infallible allocation methods.portable-atomic: useportable_atomicinstead ofcore::sync::atomic.portable-atomic-util: implement traits forportable_atomic_util::Arcinstead ofalloc::sync::Arc.raw-buffer: enableRawBufferandRawLayout.serde: implementSerializeandDeserializeforArcSliceandArcSliceMut.std: enable variousstdtrait implementations and link to the standard library crate.
Additionally, the default layout can be overridden with these features:
default-layout-any-buffer: setArcLayoutANY_BUFFERtotrue.default-layout-static: setArcLayoutSTATICtotrue.default-layout-boxed-slice: override default layout toBoxedSliceLayout.default-layout-vec: override default layout toVecLayout.default-layout-raw: override default layout toRawLayout.default-layout-mut-any-buffer: setArcLayoutANY_BUFFERtotrueforArcSliceMut.default-layout-mut-vec: override default layout toVecLayoutforArcSliceMut.
Modules§
- buffer
- Generic slice and buffer abstractions used by
ArcSliceandArcSliceMut. - error
- Error types used in fallible allocation and buffer resizing.
- inlined
inlined - Small String Optimization support for
ArcSlice. - layout
- The different layouts used by
ArcSliceandArcSliceMut.
Structs§
- ArcSlice
- A thread-safe, cheaply cloneable and sliceable container.
- ArcSlice
Borrow - A borrowed view of an
ArcSlice. - ArcSlice
Mut - A thread-safe, mutable and growable container.
Type Aliases§
- ArcBytes
- An alias for
ArcSlice<[u8], L>. - ArcBytes
Borrow - An alias for
ArcSliceBorrow<[u8], L>. - ArcBytes
Mut - An alias for
ArcSliceMut<[u8], L>. - ArcStr
- An alias for
ArcSlice<str, L>. - ArcStr
Borrow - An alias for
ArcSliceBorrow<str, L>. - ArcStr
Mut - An alias for
ArcSliceMut<str, L>.