1use std::fmt::Debug;
2use std::ops::{
3 BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Deref, RangeBounds, Sub,
4 SubAssign,
5};
6
7use bytes::{BufMut, Bytes};
8use either::Either;
9
10use crate::{
11 Encodable, PartitionRead, PartitionWrite, Splinter, SplinterRef,
12 codec::{DecodeErr, encoder::Encoder},
13 level::High,
14};
15
16#[derive(Clone)]
67pub enum CowSplinter<B> {
68 Ref(SplinterRef<B>),
70 Owned(Splinter),
72}
73
74impl<B> Default for CowSplinter<B> {
75 fn default() -> Self {
76 Self::Owned(Splinter::EMPTY)
77 }
78}
79
80impl<B: Deref<Target = [u8]>> Debug for CowSplinter<B> {
81 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82 match self {
83 CowSplinter::Ref(splinter_ref) => f
84 .debug_tuple("CowSplinter::Ref")
85 .field(splinter_ref)
86 .finish(),
87 CowSplinter::Owned(splinter) => {
88 f.debug_tuple("CowSplinter::Owned").field(splinter).finish()
89 }
90 }
91 }
92}
93
94impl<B, K: Into<u32>> FromIterator<K> for CowSplinter<B>
95where
96 B: Deref<Target = [u8]>,
97{
98 fn from_iter<I: IntoIterator<Item = K>>(iter: I) -> Self {
99 Self::Owned(Splinter::from_iter(iter.into_iter().map(|k| k.into())))
100 }
101}
102
103impl<B> From<Splinter> for CowSplinter<B> {
104 fn from(splinter: Splinter) -> Self {
105 Self::Owned(splinter)
106 }
107}
108
109impl<B> From<SplinterRef<B>> for CowSplinter<B> {
110 fn from(splinter_ref: SplinterRef<B>) -> Self {
111 Self::Ref(splinter_ref)
112 }
113}
114
115impl<B: Deref<Target = [u8]>> From<CowSplinter<B>> for Splinter {
116 fn from(cow_splinter: CowSplinter<B>) -> Self {
117 cow_splinter.into_owned()
118 }
119}
120
121impl From<CowSplinter<Bytes>> for SplinterRef<Bytes> {
122 fn from(cow: CowSplinter<Bytes>) -> Self {
123 match cow {
124 CowSplinter::Ref(splinter_ref) => splinter_ref,
125 CowSplinter::Owned(splinter) => splinter.encode_to_splinter_ref(),
126 }
127 }
128}
129
130impl<B> CowSplinter<B> {
131 pub fn from_owned(splinter: Splinter) -> Self {
143 splinter.into()
144 }
145
146 pub fn from_ref(splinter: SplinterRef<B>) -> Self {
161 splinter.into()
162 }
163}
164
165impl<B: Deref<Target = [u8]>> CowSplinter<B> {
166 pub fn from_bytes(data: B) -> Result<Self, DecodeErr> {
188 Ok(Self::Ref(SplinterRef::from_bytes(data)?))
189 }
190
191 pub fn into_owned(self) -> Splinter {
211 match self {
212 Self::Ref(splinter_ref) => splinter_ref.decode_to_splinter(),
213 Self::Owned(splinter) => splinter,
214 }
215 }
216
217 pub fn to_mut(&mut self) -> &mut Splinter {
241 match *self {
242 Self::Ref(ref splinter_ref) => {
243 *self = Self::Owned(splinter_ref.decode_to_splinter());
244 match *self {
245 Self::Ref(..) => unreachable!(),
246 Self::Owned(ref mut owned) => owned,
247 }
248 }
249 Self::Owned(ref mut owned) => owned,
250 }
251 }
252}
253
254impl CowSplinter<Bytes> {
255 pub fn encode_to_bytes(&self) -> Bytes {
274 match self {
275 CowSplinter::Ref(splinter_ref) => splinter_ref.encode_to_bytes(),
276 CowSplinter::Owned(splinter) => splinter.encode_to_bytes(),
277 }
278 }
279}
280
281impl<B: Deref<Target = [u8]>> Encodable for CowSplinter<B> {
282 fn encoded_size(&self) -> usize {
283 match self {
284 CowSplinter::Ref(splinter_ref) => splinter_ref.encoded_size(),
285 CowSplinter::Owned(splinter) => splinter.encoded_size(),
286 }
287 }
288
289 fn encode<T: BufMut>(&self, encoder: &mut Encoder<T>) {
290 match self {
291 CowSplinter::Ref(splinter_ref) => splinter_ref.encode(encoder),
292 CowSplinter::Owned(splinter) => splinter.encode(encoder),
293 }
294 }
295}
296
297impl<B: Deref<Target = [u8]>> PartitionRead<High> for CowSplinter<B> {
298 fn cardinality(&self) -> usize {
299 match self {
300 CowSplinter::Ref(splinter_ref) => splinter_ref.cardinality(),
301 CowSplinter::Owned(splinter) => splinter.cardinality(),
302 }
303 }
304
305 fn is_empty(&self) -> bool {
306 match self {
307 CowSplinter::Ref(splinter_ref) => splinter_ref.is_empty(),
308 CowSplinter::Owned(splinter) => splinter.is_empty(),
309 }
310 }
311
312 fn contains(&self, value: u32) -> bool {
313 match self {
314 CowSplinter::Ref(splinter_ref) => splinter_ref.contains(value),
315 CowSplinter::Owned(splinter) => splinter.contains(value),
316 }
317 }
318
319 fn position(&self, value: u32) -> Option<usize> {
320 match self {
321 CowSplinter::Ref(splinter_ref) => splinter_ref.position(value),
322 CowSplinter::Owned(splinter) => splinter.position(value),
323 }
324 }
325
326 fn rank(&self, value: u32) -> usize {
327 match self {
328 CowSplinter::Ref(splinter_ref) => splinter_ref.rank(value),
329 CowSplinter::Owned(splinter) => splinter.rank(value),
330 }
331 }
332
333 fn select(&self, idx: usize) -> Option<u32> {
334 match self {
335 CowSplinter::Ref(splinter_ref) => splinter_ref.select(idx),
336 CowSplinter::Owned(splinter) => splinter.select(idx),
337 }
338 }
339
340 fn last(&self) -> Option<u32> {
341 match self {
342 CowSplinter::Ref(splinter_ref) => splinter_ref.last(),
343 CowSplinter::Owned(splinter) => splinter.last(),
344 }
345 }
346
347 fn iter(&self) -> impl Iterator<Item = u32> {
348 match self {
349 CowSplinter::Ref(splinter_ref) => Either::Left(splinter_ref.iter()),
350 CowSplinter::Owned(splinter) => Either::Right(splinter.iter()),
351 }
352 }
353
354 fn contains_all<R: RangeBounds<u32>>(&self, values: R) -> bool {
355 match self {
356 CowSplinter::Ref(splinter_ref) => splinter_ref.contains_all(values),
357 CowSplinter::Owned(splinter) => splinter.contains_all(values),
358 }
359 }
360
361 fn contains_any<R: RangeBounds<u32>>(&self, values: R) -> bool {
362 match self {
363 CowSplinter::Ref(splinter_ref) => splinter_ref.contains_any(values),
364 CowSplinter::Owned(splinter) => splinter.contains_any(values),
365 }
366 }
367}
368
369impl<B: Deref<Target = [u8]>> PartitionWrite<High> for CowSplinter<B> {
370 #[inline]
371 fn insert(&mut self, value: u32) -> bool {
372 self.to_mut().insert(value)
373 }
374
375 #[inline]
376 fn remove(&mut self, value: u32) -> bool {
377 self.to_mut().remove(value)
378 }
379
380 #[inline]
381 fn remove_range<R: RangeBounds<u32>>(&mut self, values: R) {
382 self.to_mut().remove_range(values)
383 }
384}
385
386impl<B: Deref<Target = [u8]>, B2: Deref<Target = [u8]>> PartialEq<CowSplinter<B2>>
387 for CowSplinter<B>
388{
389 fn eq(&self, other: &CowSplinter<B2>) -> bool {
390 use CowSplinter::*;
391 match (self, other) {
392 (Ref(l), Ref(r)) => l == r,
393 (Ref(l), Owned(r)) => l == r,
394 (Owned(l), Ref(r)) => l == r,
395 (Owned(l), Owned(r)) => l == r,
396 }
397 }
398}
399
400impl<B: Deref<Target = [u8]>> Eq for CowSplinter<B> {}
401
402impl<B: Deref<Target = [u8]>> PartialEq<Splinter> for CowSplinter<B> {
403 fn eq(&self, other: &Splinter) -> bool {
404 other == self
405 }
406}
407
408impl<B: Deref<Target = [u8]>, B2: Deref<Target = [u8]>> PartialEq<SplinterRef<B2>>
409 for CowSplinter<B>
410{
411 fn eq(&self, other: &SplinterRef<B2>) -> bool {
412 match self {
413 CowSplinter::Ref(splinter_ref) => splinter_ref == other,
414 CowSplinter::Owned(splinter) => splinter == other,
415 }
416 }
417}
418
419macro_rules! owned_bitop {
420 ($b:ty $([$($for:tt)*])?) => {
421 owned_bitop!($b $([$($for)*])?, BitAnd, bitand, BitAndAssign::bitand_assign);
422 owned_bitop!($b $([$($for)*])?, BitOr, bitor, BitOrAssign::bitor_assign);
423 owned_bitop!($b $([$($for)*])?, BitXor, bitxor, BitXorAssign::bitxor_assign);
424 owned_bitop!($b $([$($for)*])?, Sub, sub, SubAssign::sub_assign);
425 };
426 ($b:ty $([$($for:tt)*])?, $BitOp:ident, $bitop:ident, $bitassign:path) => {
427 impl<B1: Deref<Target = [u8]> $(, $($for)*)?> $BitOp<$b> for CowSplinter<B1> {
428 type Output = Self;
429 fn $bitop(mut self, rhs: $b) -> Self::Output {
430 $bitassign(self.to_mut(), rhs);
431 self
432 }
433 }
434 };
435}
436
437owned_bitop!(Splinter);
438owned_bitop!(&Splinter);
439owned_bitop!(SplinterRef<B2> [B2: Deref<Target=[u8]>]);
440owned_bitop!(&SplinterRef<B2> [B2: Deref<Target=[u8]>]);
441owned_bitop!(CowSplinter<B2> [B2: Deref<Target=[u8]>]);
442owned_bitop!(&CowSplinter<B2> [B2: Deref<Target=[u8]>]);
443
444macro_rules! ref_bitop {
445 ($b:ty $([$($for:tt)*])?) => {
446 ref_bitop!($b $([$($for)*])?, BitAnd, bitand, BitAndAssign::bitand_assign);
447 ref_bitop!($b $([$($for)*])?, BitOr, bitor, BitOrAssign::bitor_assign);
448 ref_bitop!($b $([$($for)*])?, BitXor, bitxor, BitXorAssign::bitxor_assign);
449 ref_bitop!($b $([$($for)*])?, Sub, sub, SubAssign::sub_assign);
450 };
451 ($b:ty $([$($for:tt)*])?, $BitOp:ident, $bitop:ident, $bitassign:path) => {
452 impl<B1: Deref<Target = [u8]> + Clone $(, $($for)*)?> $BitOp<$b>
453 for &CowSplinter<B1>
454 {
455 type Output = CowSplinter<B1>;
456 fn $bitop(self, rhs: $b) -> Self::Output {
457 let mut out = self.clone();
458 $bitassign(out.to_mut(), rhs);
459 out
460 }
461 }
462 };
463}
464
465ref_bitop!(Splinter);
466ref_bitop!(&Splinter);
467ref_bitop!(SplinterRef<B2> [B2: Deref<Target=[u8]>]);
468ref_bitop!(&SplinterRef<B2> [B2: Deref<Target=[u8]>]);
469ref_bitop!(CowSplinter<B2> [B2: Deref<Target=[u8]>]);
470ref_bitop!(&CowSplinter<B2> [B2: Deref<Target=[u8]>]);