1#[cfg(feature = "dev")]
2use arbitrary::{size_hint::and_all, Arbitrary, Error as ArbitraryError, Unstructured};
3
4use core::borrow::Borrow;
8use core::convert::AsRef;
9use core::fmt::Debug;
10use core::hash::Hash;
11use core::iter;
12use core::mem::size_of;
13use core::ops::Deref;
14
15use bytes::{BufMut, Bytes, BytesMut};
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
21pub struct Component<'a, const MAX_COMPONENT_LENGTH: usize>(&'a [u8]);
22
23impl<'a, const MAX_COMPONENT_LENGTH: usize> Component<'a, MAX_COMPONENT_LENGTH> {
24 pub fn new(slice: &'a [u8]) -> Option<Self> {
26 if slice.len() <= MAX_COMPONENT_LENGTH {
27 return Some(unsafe { Self::new_unchecked(slice) }); } else {
29 None
30 }
31 }
32
33 pub unsafe fn new_unchecked(slice: &'a [u8]) -> Self {
40 Self(slice)
41 }
42
43 pub fn new_empty() -> Self {
45 Self(&[])
46 }
47
48 pub fn into_inner(self) -> &'a [u8] {
49 self.0
50 }
51}
52
53impl<'a, const MAX_COMPONENT_LENGTH: usize> Deref for Component<'a, MAX_COMPONENT_LENGTH> {
54 type Target = [u8];
55
56 fn deref(&self) -> &Self::Target {
57 self.0
58 }
59}
60
61impl<'a, const MAX_COMPONENT_LENGTH: usize> AsRef<[u8]> for Component<'a, MAX_COMPONENT_LENGTH> {
62 fn as_ref(&self) -> &[u8] {
63 self.0
64 }
65}
66
67impl<'a, const MAX_COMPONENT_LENGTH: usize> Borrow<[u8]> for Component<'a, MAX_COMPONENT_LENGTH> {
68 fn borrow(&self) -> &[u8] {
69 self.0
70 }
71}
72
73#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
77pub struct OwnedComponent<const MAX_COMPONENT_LENGTH: usize>(Bytes);
78
79impl<const MAX_COMPONENT_LENGTH: usize> OwnedComponent<MAX_COMPONENT_LENGTH> {
80 pub fn new(data: &[u8]) -> Option<Self> {
86 if data.len() <= MAX_COMPONENT_LENGTH {
87 Some(unsafe { Self::new_unchecked(data) }) } else {
89 None
90 }
91 }
92
93 pub unsafe fn new_unchecked(data: &[u8]) -> Self {
104 Self(Bytes::copy_from_slice(data))
105 }
106
107 pub fn new_empty() -> Self {
113 Self(Bytes::new())
114 }
115}
116
117impl<const MAX_COMPONENT_LENGTH: usize> Deref for OwnedComponent<MAX_COMPONENT_LENGTH> {
118 type Target = [u8];
119
120 fn deref(&self) -> &Self::Target {
121 self.0.deref()
122 }
123}
124
125impl<const MAX_COMPONENT_LENGTH: usize> AsRef<[u8]> for OwnedComponent<MAX_COMPONENT_LENGTH> {
126 fn as_ref(&self) -> &[u8] {
127 self.0.as_ref()
128 }
129}
130
131impl<const MAX_COMPONENT_LENGTH: usize> Borrow<[u8]> for OwnedComponent<MAX_COMPONENT_LENGTH> {
132 fn borrow(&self) -> &[u8] {
133 self.0.borrow()
134 }
135}
136
137#[derive(Debug)]
138pub enum InvalidPathError {
140 PathTooLong,
142 TooManyComponents,
144}
145
146impl core::fmt::Display for InvalidPathError {
147 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
148 match self {
149 InvalidPathError::PathTooLong => {
150 write!(
151 f,
152 "Total length of a path in bytes exceeded the maximum path length"
153 )
154 }
155 InvalidPathError::TooManyComponents => {
156 write!(
157 f,
158 "Number of components of a path exceeded the maximum component count"
159 )
160 }
161 }
162 }
163}
164
165impl std::error::Error for InvalidPathError {}
166
167#[derive(Clone)]
171pub struct Path<const MCL: usize, const MCC: usize, const MPL: usize> {
172 data: HeapEncoding<MCL>,
174 component_count: usize,
177}
178
179impl<const MCL: usize, const MCC: usize, const MPL: usize> Path<MCL, MCC, MPL> {
180 pub fn new_empty() -> Self {
186 Path {
187 data: HeapEncoding(Bytes::from_static(&[
189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190 ])),
191 component_count: 0,
192 }
193 }
194
195 pub fn new_singleton(comp: Component<MCL>) -> Result<Self, InvalidPathError> {
203 if 1 > MCC {
204 Err(InvalidPathError::TooManyComponents)
205 } else if comp.len() > MPL {
206 return Err(InvalidPathError::PathTooLong);
207 } else {
208 let mut buf = BytesMut::with_capacity((2 * size_of::<usize>()) + comp.len());
209 buf.extend_from_slice(&(1usize.to_ne_bytes())[..]);
210 buf.extend_from_slice(&(comp.len().to_ne_bytes())[..]);
211 buf.extend_from_slice(comp.as_ref());
212
213 return Ok(Path {
214 data: HeapEncoding(buf.freeze()),
215 component_count: 1,
216 });
217 }
218 }
219
220 pub fn new_from_iter<'a, I>(total_length: usize, iter: &mut I) -> Result<Self, InvalidPathError>
230 where
231 I: ExactSizeIterator<Item = Component<'a, MCL>>,
232 {
233 if total_length > MPL {
234 return Err(InvalidPathError::PathTooLong);
235 }
236
237 let component_count = iter.len();
238
239 if component_count > MCC {
240 return Err(InvalidPathError::TooManyComponents);
241 }
242
243 let mut buf =
244 BytesMut::with_capacity(((component_count + 1) * size_of::<usize>()) + total_length);
245 buf.extend_from_slice(&(component_count.to_ne_bytes())[..]);
246
247 buf.put_bytes(0, component_count * size_of::<usize>());
249
250 let mut accumulated_component_length = 0;
251 for (i, comp) in iter.enumerate() {
252 accumulated_component_length += comp.len();
254 let start = (1 + i) * size_of::<usize>();
255 let end = start + size_of::<usize>();
256 buf.as_mut()[start..end]
257 .copy_from_slice(&accumulated_component_length.to_ne_bytes()[..]);
258
259 buf.extend_from_slice(comp.as_ref());
261 }
262
263 if accumulated_component_length != total_length {
264 panic!("Tried to construct a path of total length {}, but got components whose accumulated length was {}.", total_length, accumulated_component_length);
265 }
266
267 Ok(Path {
268 data: HeapEncoding(buf.freeze()),
269 component_count,
270 })
271 }
272
273 pub fn new_from_slice(components: &[Component<MCL>]) -> Result<Self, InvalidPathError> {
281 let mut total_length = 0;
282 for comp in components {
283 total_length += comp.len();
284 }
285
286 return Self::new_from_iter(total_length, &mut components.iter().copied());
287 }
288
289 pub fn append(&self, comp: Component<MCL>) -> Result<Self, InvalidPathError> {
297 let total_length = self.get_path_length() + comp.len();
298 return Self::new_from_iter(
299 total_length,
300 &mut ExactLengthChain::new(self.components(), iter::once(comp)),
301 );
302 }
303
304 pub fn append_slice(&self, components: &[Component<MCL>]) -> Result<Self, InvalidPathError> {
312 let mut total_length = self.get_path_length();
313 for comp in components {
314 total_length += comp.len();
315 }
316
317 return Self::new_from_iter(
318 total_length,
319 &mut ExactLengthChain::new(self.components(), components.iter().copied()),
320 );
321 }
322
323 pub fn get_component_count(&self) -> usize {
331 self.component_count
332 }
333
334 pub fn is_empty(&self) -> bool {
340 self.get_component_count() == 0
341 }
342
343 pub fn get_path_length(&self) -> usize {
351 if self.component_count == 0 {
352 0
353 } else {
354 return HeapEncoding::<MCL>::get_sum_of_lengths_for_component(
355 self.data.as_ref(),
356 self.component_count - 1,
357 )
358 .unwrap();
359 }
360 }
361
362 pub fn get_component(&self, i: usize) -> Option<Component<MCL>> {
368 return HeapEncoding::<MCL>::get_component(self.data.as_ref(), i);
369 }
370
371 pub fn get_owned_component(&self, i: usize) -> Option<OwnedComponent<MCL>> {
377 let start = HeapEncoding::<MCL>::start_offset_of_component(self.data.0.as_ref(), i)?;
378 let end = HeapEncoding::<MCL>::end_offset_of_component(self.data.0.as_ref(), i)?;
379 Some(OwnedComponent(self.data.0.slice(start..end)))
380 }
381
382 pub fn components(
390 &self,
391 ) -> impl DoubleEndedIterator<Item = Component<MCL>> + ExactSizeIterator<Item = Component<MCL>>
392 {
393 self.suffix_components(0)
394 }
395
396 pub fn suffix_components(
404 &self,
405 i: usize,
406 ) -> impl DoubleEndedIterator<Item = Component<MCL>> + ExactSizeIterator<Item = Component<MCL>>
407 {
408 (i..self.get_component_count()).map(|i| {
409 self.get_component(i).unwrap() })
411 }
412
413 pub fn owned_components(
421 &self,
422 ) -> impl DoubleEndedIterator<Item = OwnedComponent<MCL>>
423 + ExactSizeIterator<Item = OwnedComponent<MCL>>
424 + '_ {
425 self.suffix_owned_components(0)
426 }
427
428 pub fn suffix_owned_components(
436 &self,
437 i: usize,
438 ) -> impl DoubleEndedIterator<Item = OwnedComponent<MCL>>
439 + ExactSizeIterator<Item = OwnedComponent<MCL>>
440 + '_ {
441 (i..self.get_component_count()).map(|i| {
442 self.get_owned_component(i).unwrap() })
444 }
445
446 pub fn create_prefix(&self, length: usize) -> Option<Self> {
454 if length > self.get_component_count() {
455 None
456 } else {
457 Some(unsafe { self.create_prefix_unchecked(length) })
458 }
459 }
460
461 pub unsafe fn create_prefix_unchecked(&self, length: usize) -> Self {
472 Self {
473 data: self.data.clone(),
474 component_count: length,
475 }
476 }
477
478 pub fn all_prefixes(&self) -> impl DoubleEndedIterator<Item = Self> + '_ {
486 (0..=self.get_component_count()).map(|i| {
487 unsafe {
488 self.create_prefix_unchecked(i) }
490 })
491 }
492
493 pub fn is_prefix_of(&self, other: &Self) -> bool {
500 for (comp_a, comp_b) in self.components().zip(other.components()) {
501 if comp_a != comp_b {
502 return false;
503 }
504 }
505
506 self.get_component_count() <= other.get_component_count()
507 }
508
509 pub fn is_prefixed_by(&self, other: &Self) -> bool {
516 other.is_prefix_of(self)
517 }
518
519 pub fn longest_common_prefix(&self, other: &Self) -> Self {
525 let mut lcp_len = 0;
526
527 for (comp_a, comp_b) in self.components().zip(other.components()) {
528 if comp_a != comp_b {
529 break;
530 }
531
532 lcp_len += 1
533 }
534
535 self.create_prefix(lcp_len).unwrap() }
537
538 pub fn successor(&self) -> Option<Self> {
544 if let Ok(path) = self.append(Component::new_empty()) {
546 return Some(path);
547 }
548
549 for (i, component) in self.components().enumerate().rev() {
555 if component.len() < MCL
560 && HeapEncoding::<MCL>::get_sum_of_lengths_for_component(self.data.as_ref(), i)
561 .unwrap() < MPL
563 {
564 let mut buf = clone_prefix_and_lengthen_final_component(self, i, 1);
568 buf.put_u8(0);
569
570 return Some(Self::from_buffer_and_component_count(buf.freeze(), i + 1));
571 }
572
573 let can_increment = !component.iter().all(|byte| *byte == 255);
575
576 if can_increment {
579 let mut buf = clone_prefix_and_lengthen_final_component(self, i, 0);
580
581 let start_component_offset =
582 HeapEncoding::<MCL>::start_offset_of_component(buf.as_ref(), i).unwrap(); let end_component_offset =
584 HeapEncoding::<MCL>::end_offset_of_component(buf.as_ref(), i).unwrap(); fixed_width_increment(
586 &mut buf.as_mut()[start_component_offset..end_component_offset],
587 );
588
589 return Some(Self::from_buffer_and_component_count(buf.freeze(), i + 1));
590 }
591 }
592
593 None
595 }
596
597 pub fn greater_but_not_prefixed(&self) -> Option<Self> {
603 for (i, component) in self.components().enumerate().rev() {
606 if component.len() < MCL
608 && HeapEncoding::<MCL>::get_sum_of_lengths_for_component(self.data.as_ref(), i)
609 .unwrap() < MPL
611 {
612 let mut buf = clone_prefix_and_lengthen_final_component(self, i, 1);
613 buf.put_u8(0);
614
615 return Some(Self::from_buffer_and_component_count(buf.freeze(), i + 1));
616 }
617
618 let mut next_component_length = None;
620 for (j, comp_byte) in component.iter().enumerate().rev() {
621 if *comp_byte < 255 {
622 next_component_length = Some(j + 1);
623 break;
624 }
625 }
626
627 if let Some(next_component_length) = next_component_length {
628 let mut buf = clone_prefix_and_lengthen_final_component(self, i, 0);
631 let length_of_prefix =
632 HeapEncoding::<MCL>::get_sum_of_lengths_for_component(&buf, i).unwrap();
633
634 buf_set_final_component_length(
636 buf.as_mut(),
637 i,
638 length_of_prefix - (component.len() - next_component_length),
639 );
640
641 let offset = HeapEncoding::<MCL>::start_offset_of_component(buf.as_ref(), i)
643 .unwrap()
644 + next_component_length
645 - 1;
646 let byte = buf.as_ref()[offset]; buf.as_mut()[offset] = byte + 1; return Some(Self::from_buffer_and_component_count(buf.freeze(), i + 1));
650 }
651 }
652
653 None
654 }
655
656 pub(crate) fn from_buffer_and_component_count(buf: Bytes, component_count: usize) -> Self {
657 Path {
658 data: HeapEncoding(buf),
659 component_count,
660 }
661 }
662
663 pub(crate) fn raw_buf(&self) -> &[u8] {
664 self.data.0.as_ref()
665 }
666}
667
668impl<const MCL: usize, const MCC: usize, const MPL: usize> PartialEq for Path<MCL, MCC, MPL> {
669 fn eq(&self, other: &Self) -> bool {
670 if self.component_count != other.component_count {
671 false
672 } else {
673 return self.components().eq(other.components());
674 }
675 }
676}
677
678impl<const MCL: usize, const MCC: usize, const MPL: usize> Eq for Path<MCL, MCC, MPL> {}
679
680impl<const MCL: usize, const MCC: usize, const MPL: usize> Hash for Path<MCL, MCC, MPL> {
681 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
682 self.component_count.hash(state);
683
684 for comp in self.components() {
685 comp.hash(state);
686 }
687 }
688}
689
690impl<const MCL: usize, const MCC: usize, const MPL: usize> PartialOrd for Path<MCL, MCC, MPL> {
691 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
692 Some(self.cmp(other))
693 }
694}
695
696impl<const MCL: usize, const MCC: usize, const MPL: usize> Ord for Path<MCL, MCC, MPL> {
698 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
699 return self.components().cmp(other.components());
700 }
701}
702
703impl<const MCL: usize, const MCC: usize, const MPL: usize> Debug for Path<MCL, MCC, MPL> {
704 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
705 let data_vec: Vec<_> = self.components().collect();
706
707 f.debug_tuple("Path").field(&data_vec).finish()
708 }
709}
710
711#[derive(Clone)]
720struct HeapEncoding<const MAX_COMPONENT_LENGTH: usize>(Bytes);
721
722impl<const MAX_COMPONENT_LENGTH: usize> HeapEncoding<MAX_COMPONENT_LENGTH> {
725 fn get_component_count(buf: &[u8]) -> usize {
726 Self::get_usize_at_offset(buf, 0).unwrap() }
728
729 fn get_sum_of_lengths_for_component(buf: &[u8], i: usize) -> Option<usize> {
731 let start_offset_in_bytes = Self::start_offset_of_sum_of_lengths_for_component(i);
732 Self::get_usize_at_offset(buf, start_offset_in_bytes)
733 }
734
735 fn get_component(buf: &[u8], i: usize) -> Option<Component<MAX_COMPONENT_LENGTH>> {
736 let start = Self::start_offset_of_component(buf, i)?;
737 let end = Self::end_offset_of_component(buf, i)?;
738 return Some(unsafe { Component::new_unchecked(&buf[start..end]) });
739 }
740
741 fn start_offset_of_sum_of_lengths_for_component(i: usize) -> usize {
742 size_of::<usize>() * (i + 1)
744 }
745
746 fn start_offset_of_component(buf: &[u8], i: usize) -> Option<usize> {
747 let metadata_length = (Self::get_component_count(buf) + 1) * size_of::<usize>();
748 if i == 0 {
749 Some(metadata_length)
750 } else {
751 Self::get_sum_of_lengths_for_component(buf, i - 1) .map(|length| length + metadata_length)
753 }
754 }
755
756 fn end_offset_of_component(buf: &[u8], i: usize) -> Option<usize> {
757 let metadata_length = (Self::get_component_count(buf) + 1) * size_of::<usize>();
758 Self::get_sum_of_lengths_for_component(buf, i).map(|length| length + metadata_length)
759 }
760
761 fn get_usize_at_offset(buf: &[u8], offset_in_bytes: usize) -> Option<usize> {
762 let end = offset_in_bytes + size_of::<usize>();
763
764 let mut usize_bytes = [0u8; size_of::<usize>()];
767
768 if buf.len() < end {
769 None
770 } else {
771 usize_bytes.copy_from_slice(&buf[offset_in_bytes..end]);
772 Some(usize::from_ne_bytes(usize_bytes))
773 }
774 }
775}
776
777impl<const MAX_COMPONENT_LENGTH: usize> AsRef<[u8]> for HeapEncoding<MAX_COMPONENT_LENGTH> {
778 fn as_ref(&self) -> &[u8] {
779 self.0.as_ref()
780 }
781}
782
783use syncify::syncify;
784use syncify::syncify_replace;
785
786#[syncify(encoding_sync)]
787mod encoding {
788 use super::*;
789
790 #[syncify_replace(use ufotofu::sync::{BulkConsumer, BulkProducer};)]
791 use ufotofu::local_nb::{BulkConsumer, BulkProducer};
792
793 use willow_encoding::DecodeError;
794 #[syncify_replace(use willow_encoding::sync::{Decodable, Encodable};)]
795 use willow_encoding::{Decodable, Encodable};
796
797 #[syncify_replace(use willow_encoding::sync::{decode_max_power, encode_max_power};)]
798 use willow_encoding::{decode_max_power, encode_max_power};
799
800 impl<const MCL: usize, const MCC: usize, const MPL: usize> Encodable for Path<MCL, MCC, MPL> {
801 async fn encode<C>(&self, consumer: &mut C) -> Result<(), C::Error>
802 where
803 C: BulkConsumer<Item = u8>,
804 {
805 encode_max_power(self.get_component_count(), MCC, consumer).await?;
806
807 for component in self.components() {
808 encode_max_power(component.len(), MCL, consumer).await?;
809
810 consumer
811 .bulk_consume_full_slice(component.as_ref())
812 .await
813 .map_err(|f| f.reason)?;
814 }
815
816 Ok(())
817 }
818 }
819
820 impl<const MCL: usize, const MCC: usize, const MPL: usize> Decodable for Path<MCL, MCC, MPL> {
821 async fn decode<P>(producer: &mut P) -> Result<Self, DecodeError<P::Error>>
822 where
823 P: BulkProducer<Item = u8>,
824 {
825 let component_count: usize = decode_max_power(MCC, producer).await?.try_into()?;
826 if component_count > MCC {
827 return Err(DecodeError::InvalidInput);
828 }
829
830 let mut buf = ScratchSpacePathDecoding::<MCC, MPL>::new();
831
832 let mut accumulated_component_length: usize = 0; for i in 0..component_count {
834 let component_len: usize = decode_max_power(MCL, producer).await?.try_into()?;
835 if component_len > MCL {
836 return Err(DecodeError::InvalidInput);
837 }
838
839 accumulated_component_length += component_len;
840 if accumulated_component_length > MPL {
841 return Err(DecodeError::InvalidInput);
842 }
843
844 buf.set_component_accumulated_length(accumulated_component_length, i);
845
846 producer
848 .bulk_overwrite_full_slice(unsafe {
849 buf.path_data_as_mut(i)
851 })
852 .await?;
853 }
854
855 Ok(unsafe { buf.to_path(component_count) })
856 }
857 }
858}
859
860#[cfg(feature = "dev")]
861impl<'a, const MCL: usize, const MCC: usize, const MPL: usize> Arbitrary<'a>
862 for Path<MCL, MCC, MPL>
863{
864 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, ArbitraryError> {
865 let mut total_length_in_bytes: usize = Arbitrary::arbitrary(u)?;
866 total_length_in_bytes %= MPL + 1;
867
868 let data: Vec<u8> = Arbitrary::arbitrary(u)?;
869 total_length_in_bytes = core::cmp::min(total_length_in_bytes, data.len());
870
871 let mut num_components: usize = Arbitrary::arbitrary(u)?;
872 num_components %= MCC + 1;
873
874 if num_components == 0 {
875 total_length_in_bytes = 0;
876 }
877
878 let buf_capacity = size_of::<usize>() * (1 + num_components) + total_length_in_bytes;
879 let mut buf = BytesMut::with_capacity(buf_capacity);
880
881 buf.extend_from_slice(&num_components.to_ne_bytes());
883
884 let mut length_total_so_far = 0;
885 for i in 0..num_components {
886 if i + 1 == num_components {
888 let final_component_length = total_length_in_bytes - length_total_so_far;
889
890 if final_component_length > MCL {
891 return Err(ArbitraryError::IncorrectFormat);
892 } else {
893 buf.extend_from_slice(&total_length_in_bytes.to_ne_bytes());
894 }
895 } else {
896 let mut component_length: usize = Arbitrary::arbitrary(u)?;
898 component_length %= MCL + 1;
900 component_length = core::cmp::min(
902 component_length,
903 total_length_in_bytes - length_total_so_far,
904 );
905
906 length_total_so_far += component_length;
907 buf.extend_from_slice(&length_total_so_far.to_ne_bytes());
908 }
909 }
910
911 buf.extend_from_slice(&data);
913
914 Ok(Path {
915 data: HeapEncoding(buf.freeze()),
916 component_count: num_components,
917 })
918 }
919
920 #[inline]
921 fn size_hint(depth: usize) -> (usize, Option<usize>) {
922 (
923 and_all(&[
924 usize::size_hint(depth),
925 usize::size_hint(depth),
926 Vec::<u8>::size_hint(depth),
927 ])
928 .0,
929 None,
930 )
931 }
932}
933
934struct ExactLengthChain<A, B> {
938 a: Option<A>,
939 b: Option<B>,
940}
941
942impl<A, B> ExactLengthChain<A, B> {
943 fn new(a: A, b: B) -> ExactLengthChain<A, B> {
944 ExactLengthChain {
945 a: Some(a),
946 b: Some(b),
947 }
948 }
949}
950
951impl<A, B> Iterator for ExactLengthChain<A, B>
952where
953 A: Iterator,
954 B: Iterator<Item = A::Item>,
955{
956 type Item = A::Item;
957
958 #[inline]
959 fn next(&mut self) -> Option<A::Item> {
960 and_then_or_clear(&mut self.a, Iterator::next).or_else(|| self.b.as_mut()?.next())
961 }
962
963 fn size_hint(&self) -> (usize, Option<usize>) {
964 let (lower_a, higher_a) = self.a.as_ref().map_or((0, None), |a| a.size_hint());
965 let (lower_b, higher_b) = self.b.as_ref().map_or((0, None), |b| b.size_hint());
966
967 let higher = match (higher_a, higher_b) {
968 (Some(a), Some(b)) => Some(
969 a.checked_add(b)
970 .expect("Some of lengths of two iterators must not overflow."),
971 ),
972 _ => None,
973 };
974
975 (lower_a + lower_b, higher)
976 }
977}
978
979impl<A, B> DoubleEndedIterator for ExactLengthChain<A, B>
980where
981 A: DoubleEndedIterator,
982 B: DoubleEndedIterator<Item = A::Item>,
983{
984 #[inline]
985 fn next_back(&mut self) -> Option<A::Item> {
986 and_then_or_clear(&mut self.b, |b| b.next_back()).or_else(|| self.a.as_mut()?.next_back())
987 }
988}
989
990impl<A, B> ExactSizeIterator for ExactLengthChain<A, B>
991where
992 A: ExactSizeIterator,
993 B: ExactSizeIterator<Item = A::Item>,
994{
995}
996
997#[inline]
998fn and_then_or_clear<T, U>(opt: &mut Option<T>, f: impl FnOnce(&mut T) -> Option<U>) -> Option<U> {
999 let x = f(opt.as_mut()?);
1000 if x.is_none() {
1001 *opt = None;
1002 }
1003 x
1004}
1005
1006fn buf_set_final_component_length(buf: &mut [u8], i: usize, new_sum_of_lengths: usize) {
1008 let comp_len_start = (1 + i) * size_of::<usize>();
1009 let comp_len_end = comp_len_start + size_of::<usize>();
1010 buf[comp_len_start..comp_len_end].copy_from_slice(&new_sum_of_lengths.to_ne_bytes()[..]);
1011}
1012
1013fn fixed_width_increment(buf: &mut [u8]) {
1015 for byte_ref in buf.iter_mut().rev() {
1016 if *byte_ref == 255 {
1017 *byte_ref = 0;
1018 } else {
1019 *byte_ref += 1;
1020 return;
1021 }
1022 }
1023}
1024
1025fn clone_prefix_and_lengthen_final_component<
1027 const MCL: usize,
1028 const MCC: usize,
1029 const MPL: usize,
1030>(
1031 original: &Path<MCL, MCC, MPL>,
1032 i: usize,
1033 extra_capacity: usize,
1034) -> BytesMut {
1035 let original_slice = original.data.as_ref();
1036 let successor_path_length =
1037 HeapEncoding::<MCL>::get_sum_of_lengths_for_component(original_slice, i).unwrap()
1038 + extra_capacity;
1039 let buf_capacity = size_of::<usize>() * (i + 2) + successor_path_length;
1040 let mut buf = BytesMut::with_capacity(buf_capacity);
1041
1042 buf.extend_from_slice(&(i + 1).to_ne_bytes());
1044
1045 buf.extend_from_slice(&original_slice[size_of::<usize>()..size_of::<usize>() * (i + 2)]);
1047
1048 buf_set_final_component_length(buf.as_mut(), i, successor_path_length);
1050
1051 buf.extend_from_slice(
1053 &original_slice[HeapEncoding::<MCL>::start_offset_of_component(original_slice, 0).unwrap()
1054 ..HeapEncoding::<MCL>::start_offset_of_component(original_slice, i + 1).unwrap()],
1055 );
1056
1057 buf
1058}
1059
1060#[derive(Debug)]
1062pub(crate) struct ScratchSpacePathDecoding<const MCC: usize, const MPL: usize> {
1063 component_accumulated_lengths: [usize; MCC],
1065 path_data: [u8; MPL],
1066}
1067
1068impl<const MCC: usize, const MPL: usize> ScratchSpacePathDecoding<MCC, MPL> {
1069 pub fn new() -> Self {
1070 ScratchSpacePathDecoding {
1071 component_accumulated_lengths: [0; MCC],
1072 path_data: [0; MPL],
1073 }
1074 }
1075
1076 pub fn set_component_accumulated_length(
1078 &mut self,
1079 component_accumulated_length: usize,
1080 i: usize,
1081 ) {
1082 self.component_accumulated_lengths[i] = component_accumulated_length;
1083 }
1084
1085 pub unsafe fn set_many_component_accumulated_lengths_from_ne(&mut self, lengths: &[u8]) {
1089 let slice: &mut [u8] = core::slice::from_raw_parts_mut(
1090 self.component_accumulated_lengths[..lengths.len() / size_of::<usize>()].as_mut_ptr()
1091 as *mut u8,
1092 lengths.len(),
1093 );
1094
1095 slice.copy_from_slice(lengths);
1096 }
1097
1098 unsafe fn get_component_accumulated_length(&self, i: usize) -> usize {
1102 self.component_accumulated_lengths[i]
1103 }
1104
1105 pub unsafe fn get_accumumulated_component_lengths(&self, i: usize) -> &[u8] {
1111 core::slice::from_raw_parts(
1112 self.component_accumulated_lengths[..i].as_ptr() as *const u8,
1113 i * size_of::<usize>(),
1114 )
1115 }
1116
1117 pub unsafe fn path_data_as_mut(&mut self, i: usize) -> &mut [u8] {
1123 let start = if i == 0 {
1124 0
1125 } else {
1126 self.get_component_accumulated_length(i - 1)
1127 };
1128 let end = self.get_component_accumulated_length(i);
1129 &mut self.path_data[start..end]
1130 }
1131
1132 pub unsafe fn path_data_until_as_mut(&mut self, i: usize) -> &mut [u8] {
1138 let end = self.get_component_accumulated_length(i - 1);
1139 &mut self.path_data[0..end]
1140 }
1141
1142 unsafe fn get_path_data(&self, i: usize) -> &[u8] {
1149 let end = if i == 0 {
1150 0
1151 } else {
1152 self.get_component_accumulated_length(i - 1)
1153 };
1154 &self.path_data[..end]
1155 }
1156
1157 pub unsafe fn to_path<const MCL: usize>(&self, i: usize) -> Path<MCL, MCC, MPL> {
1163 if i == 0 {
1164 Path::new_empty()
1165 } else {
1166 let total_length = if i == 0 {
1167 0
1168 } else {
1169 self.get_component_accumulated_length(i - 1)
1170 };
1171 let mut buf = BytesMut::with_capacity((size_of::<usize>() * (i + 1)) + total_length);
1172
1173 buf.extend_from_slice(&i.to_ne_bytes());
1174 buf.extend_from_slice(self.get_accumumulated_component_lengths(i));
1175 buf.extend_from_slice(self.get_path_data(i));
1176
1177 Path::from_buffer_and_component_count(buf.freeze(), i)
1178 }
1179 }
1180}