pub enum CowSplinter<B> {
Ref(SplinterRef<B>),
Owned(Splinter),
}
Expand description
A clone-on-write splinter that can hold either a reference or an owned value.
CowSplinter
is an enum that can contain either a SplinterRef<B>
(for zero-copy
read-only access) or an owned Splinter
(for mutable operations). It automatically
converts from read-only to owned when mutation is needed, providing an efficient
way to work with splinter data that might come from different sources.
This is particularly useful when you want to:
- Start with serialized/borrowed data for read-only operations
- Potentially modify the data later
- Avoid unnecessary copying until mutation is actually needed
- Or work with a collection of Splinters which may or may not be owned
§Examples
Starting with a reference and converting to owned when needed:
use splinter_rs::{Splinter, CowSplinter, PartitionWrite, PartitionRead, Encodable};
// Create a CowSplinter from a reference
let original = Splinter::from_iter([100, 200]);
let splinter_ref = original.encode_to_splinter_ref();
let mut cow = CowSplinter::from_ref(splinter_ref);
// Read operations work on the reference
assert_eq!(cow.cardinality(), 2);
// First write operation converts to owned
cow.insert(300); // This triggers clone-on-write
assert_eq!(cow.cardinality(), 3);
Creating from different sources:
use splinter_rs::{Splinter, CowSplinter, PartitionWrite, PartitionRead, Encodable};
use bytes::Bytes;
// From owned splinter
let owned = Splinter::EMPTY;
let cow1: CowSplinter<Bytes> = CowSplinter::from_owned(owned);
// From iterator
let cow2 = CowSplinter::<Bytes>::from_iter([1u32, 2, 3]);
// From bytes
let bytes = cow2.encode_to_bytes();
let cow3 = CowSplinter::from_bytes(bytes).unwrap();
Variants§
Ref(SplinterRef<B>)
Contains a zero-copy reference to serialized data
Owned(Splinter)
Contains an owned, mutable splinter
Implementations§
Source§impl<B> CowSplinter<B>
impl<B> CowSplinter<B>
Sourcepub fn from_owned(splinter: Splinter) -> Self
pub fn from_owned(splinter: Splinter) -> Self
Sourcepub fn from_ref(splinter: SplinterRef<B>) -> Self
pub fn from_ref(splinter: SplinterRef<B>) -> Self
Creates a CowSplinter
from a SplinterRef
.
§Examples
use splinter_rs::{Splinter, CowSplinter, PartitionWrite, PartitionRead, Encodable};
let mut splinter = Splinter::EMPTY;
splinter.insert(42);
let splinter_ref = splinter.encode_to_splinter_ref();
let cow = CowSplinter::from_ref(splinter_ref);
assert!(cow.contains(42));
Source§impl<B: Deref<Target = [u8]>> CowSplinter<B>
impl<B: Deref<Target = [u8]>> CowSplinter<B>
Sourcepub fn from_bytes(data: B) -> Result<Self, Culprit<DecodeErr>>
pub fn from_bytes(data: B) -> Result<Self, Culprit<DecodeErr>>
Creates a CowSplinter
from raw bytes, validating the format.
This is equivalent to creating a SplinterRef
from the bytes and wrapping
it in a CowSplinter::Ref
. All the same validation rules apply.
§Errors
Returns the same errors as SplinterRef::from_bytes
.
§Examples
use splinter_rs::{Splinter, CowSplinter, PartitionWrite, PartitionRead, Encodable};
let mut splinter = Splinter::EMPTY;
splinter.insert(42);
let bytes = splinter.encode_to_bytes();
let cow = CowSplinter::from_bytes(bytes).unwrap();
assert!(cow.contains(42));
Sourcepub fn into_owned(self) -> Splinter
pub fn into_owned(self) -> Splinter
Converts this CowSplinter
into an owned Splinter
.
If this is already an owned splinter, it returns it directly. If this is a reference, it deserializes the data into a new owned splinter.
§Examples
use splinter_rs::{Splinter, CowSplinter, PartitionWrite, PartitionRead, Encodable};
let mut original = Splinter::EMPTY;
original.insert(100);
let splinter_ref = original.encode_to_splinter_ref();
let cow = CowSplinter::from_ref(splinter_ref);
let owned = cow.into_owned();
assert_eq!(owned.cardinality(), 1);
assert!(owned.contains(100));
Sourcepub fn to_mut(&mut self) -> &mut Splinter
pub fn to_mut(&mut self) -> &mut Splinter
Returns a mutable reference to the underlying Splinter
.
This method implements the “clone-on-write” behavior: if the current value
is a Ref
, it will be converted to Owned
by deserializing the data.
Subsequent calls will return the same mutable reference without additional
conversions.
§Examples
use splinter_rs::{Splinter, CowSplinter, PartitionWrite, PartitionRead, Encodable};
let mut original = Splinter::EMPTY;
original.insert(100);
let splinter_ref = original.encode_to_splinter_ref();
let mut cow = CowSplinter::from_ref(splinter_ref);
// This triggers the clone-on-write conversion
let mutable_ref = cow.to_mut();
mutable_ref.insert(200);
assert_eq!(cow.cardinality(), 2);
Source§impl CowSplinter<Bytes>
impl CowSplinter<Bytes>
Sourcepub fn encode_to_bytes(&self) -> Bytes
pub fn encode_to_bytes(&self) -> Bytes
Returns the serialized bytes representation of this splinter.
For the Ref
variant, this clones the underlying Bytes
(which is efficient
due to reference counting). For the Owned
variant, this encodes the splinter
to bytes.
§Examples
use splinter_rs::{Splinter, CowSplinter, PartitionWrite, PartitionRead, Encodable};
let mut splinter = Splinter::EMPTY;
splinter.insert(42);
let cow = CowSplinter::from_owned(splinter);
let bytes = cow.encode_to_bytes();
assert!(!bytes.is_empty());
Trait Implementations§
Source§impl<B: Clone> Clone for CowSplinter<B>
impl<B: Clone> Clone for CowSplinter<B>
Source§fn clone(&self) -> CowSplinter<B>
fn clone(&self) -> CowSplinter<B>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<B> Default for CowSplinter<B>
impl<B> Default for CowSplinter<B>
Source§impl<B: Deref<Target = [u8]>> Encodable for CowSplinter<B>
impl<B: Deref<Target = [u8]>> Encodable for CowSplinter<B>
Source§impl<B: Deref<Target = [u8]>> From<CowSplinter<B>> for Splinter
impl<B: Deref<Target = [u8]>> From<CowSplinter<B>> for Splinter
Source§fn from(cow_splinter: CowSplinter<B>) -> Self
fn from(cow_splinter: CowSplinter<B>) -> Self
Source§impl From<CowSplinter<Bytes>> for SplinterRef<Bytes>
impl From<CowSplinter<Bytes>> for SplinterRef<Bytes>
Source§fn from(cow: CowSplinter<Bytes>) -> Self
fn from(cow: CowSplinter<Bytes>) -> Self
Source§impl<B> From<Splinter> for CowSplinter<B>
impl<B> From<Splinter> for CowSplinter<B>
Source§impl<B> From<SplinterRef<B>> for CowSplinter<B>
impl<B> From<SplinterRef<B>> for CowSplinter<B>
Source§fn from(splinter_ref: SplinterRef<B>) -> Self
fn from(splinter_ref: SplinterRef<B>) -> Self
Source§impl<B, K: Into<u32>> FromIterator<K> for CowSplinter<B>
impl<B, K: Into<u32>> FromIterator<K> for CowSplinter<B>
Source§fn from_iter<I: IntoIterator<Item = K>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = K>>(iter: I) -> Self
Source§impl<B: Deref<Target = [u8]>> Merge<CowSplinter<B>> for Splinter
impl<B: Deref<Target = [u8]>> Merge<CowSplinter<B>> for Splinter
Source§fn merge(&mut self, rhs: &CowSplinter<B>)
fn merge(&mut self, rhs: &CowSplinter<B>)
Source§impl<B: Deref<Target = [u8]>, B2: Deref<Target = [u8]>> PartialEq<CowSplinter<B2>> for CowSplinter<B>
impl<B: Deref<Target = [u8]>, B2: Deref<Target = [u8]>> PartialEq<CowSplinter<B2>> for CowSplinter<B>
Source§impl<B: Deref<Target = [u8]>> PartitionRead<High> for CowSplinter<B>
impl<B: Deref<Target = [u8]>> PartitionRead<High> for CowSplinter<B>
Source§fn cardinality(&self) -> usize
fn cardinality(&self) -> usize
Source§fn contains(&self, value: u32) -> bool
fn contains(&self, value: u32) -> bool
Source§fn rank(&self, value: u32) -> usize
fn rank(&self, value: u32) -> usize
Source§impl<B: Deref<Target = [u8]>> PartitionWrite<High> for CowSplinter<B>
impl<B: Deref<Target = [u8]>> PartitionWrite<High> for CowSplinter<B>
impl<B: Deref<Target = [u8]>> Eq for CowSplinter<B>
Auto Trait Implementations§
impl<B> Freeze for CowSplinter<B>where
B: Freeze,
impl<B> RefUnwindSafe for CowSplinter<B>where
B: RefUnwindSafe,
impl<B> Send for CowSplinter<B>where
B: Send,
impl<B> Sync for CowSplinter<B>where
B: Sync,
impl<B> Unpin for CowSplinter<B>where
B: Unpin,
impl<B> UnwindSafe for CowSplinter<B>where
B: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.