1use crate::format::{ChunkRef, FileList, FolderList, Kind, Record, Target};
13use crate::hash::{Hash, Root};
14use crate::threads::on_threads;
15use crate::wire::FRAME_CHUNK;
16use crate::{Error, Result};
17use std::collections::HashMap;
18use std::sync::atomic::{AtomicUsize, Ordering};
19use std::sync::{Arc, Mutex, RwLock};
20
21#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
23pub enum Stored {
24 Chunk,
26 FileList,
28 FolderList,
30 Record,
32}
33
34pub trait Store: Send + Sync {
41 fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()>;
44
45 fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>>;
47
48 fn lend(
59 &self,
60 kind: Stored,
61 hash: &Hash,
62 with: &mut dyn FnMut(&[u8]) -> Result<()>,
63 ) -> Result<bool> {
64 match self.get(kind, hash)? {
65 Some(bytes) => with(&bytes).map(|()| true),
66 None => Ok(false),
67 }
68 }
69
70 fn read_into(&self, kind: Stored, hash: &Hash, into: &mut [u8]) -> Result<Option<u64>> {
82 let mut length = None;
83 let held = self.lend(kind, hash, &mut |bytes| {
84 length = Some(bytes.len() as u64);
85 if bytes.len() == into.len() {
86 into.copy_from_slice(bytes);
87 }
88 Ok(())
89 })?;
90 Ok(length.filter(|_| held))
91 }
92
93 fn has(&self, kind: Stored, hash: &Hash) -> Result<bool> {
97 Ok(self.size(kind, hash)?.is_some())
98 }
99
100 fn size(&self, kind: Stored, hash: &Hash) -> Result<Option<u64>> {
102 Ok(self.get(kind, hash)?.map(|bytes| bytes.len() as u64))
103 }
104
105 fn remove(&self, kind: Stored, hash: &Hash) -> Result<()>;
107}
108
109type Things = HashMap<(Stored, Hash), Arc<[u8]>>;
111
112#[derive(Default)]
114pub struct MemoryStore {
115 things: RwLock<Things>,
116}
117
118impl MemoryStore {
119 pub fn new() -> Self {
120 Self::default()
121 }
122
123 fn read(&self) -> std::sync::RwLockReadGuard<'_, Things> {
124 self.things
125 .read()
126 .unwrap_or_else(|poisoned| poisoned.into_inner())
127 }
128
129 fn write(&self) -> std::sync::RwLockWriteGuard<'_, Things> {
130 self.things
131 .write()
132 .unwrap_or_else(|poisoned| poisoned.into_inner())
133 }
134
135 pub fn count(&self, kind: Stored) -> usize {
137 self.read().keys().filter(|key| key.0 == kind).count()
138 }
139
140 pub fn bytes(&self, kind: Stored) -> u64 {
142 self.read()
143 .iter()
144 .filter(|(key, _)| key.0 == kind)
145 .map(|(_, bytes)| bytes.len() as u64)
146 .sum()
147 }
148
149 pub fn hashes(&self, kind: Stored) -> Vec<Hash> {
151 let mut hashes: Vec<Hash> = self
152 .read()
153 .keys()
154 .filter(|key| key.0 == kind)
155 .map(|key| key.1)
156 .collect();
157 hashes.sort_unstable();
158 hashes
159 }
160}
161
162impl Store for MemoryStore {
163 fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
164 if self.read().contains_key(&(kind, *hash)) {
165 return Ok(());
166 }
167 let kept: Arc<[u8]> = Arc::from(bytes);
169 self.write().entry((kind, *hash)).or_insert(kept);
170 Ok(())
171 }
172
173 fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
174 Ok(self.read().get(&(kind, *hash)).cloned())
175 }
176
177 fn has(&self, kind: Stored, hash: &Hash) -> Result<bool> {
178 Ok(self.read().contains_key(&(kind, *hash)))
179 }
180
181 fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
182 self.write().remove(&(kind, *hash));
183 Ok(())
184 }
185}
186
187pub(crate) fn damaged(what: &'static str, hash: &Hash) -> Error {
189 Error::Corrupt { what, hash: *hash }
190}
191
192pub(crate) fn load_record(store: &dyn Store, root: &Root) -> Result<Option<Record>> {
194 let Some(bytes) = store.get(Stored::Record, root.as_hash())? else {
195 return Ok(None);
196 };
197 if Root::of_record(&bytes) != *root {
198 return Err(damaged("a stored record", root.as_hash()));
199 }
200 Record::decode(&bytes).map(Some)
201}
202
203fn confirm_size(read: u64, wanted: &Target, what: &'static str) -> Result<()> {
205 if read != wanted.size {
206 return Err(Error::SizeMismatch {
207 what,
208 expected: wanted.size,
209 actual: read,
210 });
211 }
212 Ok(())
213}
214
215pub(crate) fn load_folder(store: &dyn Store, folder: &Target) -> Result<Option<FolderList>> {
218 folder.require(Kind::Folder)?;
219 let Some(bytes) = store.get(Stored::FolderList, &folder.hash)? else {
220 return Ok(None);
221 };
222 let what = "a stored folder's list";
223 let list = FolderList::decode(&bytes)
224 .ok()
225 .filter(|list| *list.hash() == folder.hash)
226 .ok_or_else(|| damaged(what, &folder.hash))?;
227 confirm_size(list.size(), folder, what)?;
228 Ok(Some(list))
229}
230
231pub(crate) fn load_file(store: &dyn Store, file: &Target) -> Result<Option<FileList>> {
234 file.require(Kind::File)?;
235 let Some(bytes) = store.get(Stored::FileList, &file.hash)? else {
236 return Ok(None);
237 };
238 let what = "a stored file's list";
239 let list = FileList::decode(&bytes)
240 .ok()
241 .filter(|list| *list.hash() == file.hash)
242 .ok_or_else(|| damaged(what, &file.hash))?;
243 confirm_size(list.size(), file, what)?;
244 Ok(Some(list))
245}
246
247pub(crate) fn check_chunk(item: &ChunkRef, bytes: &[u8], what: &'static str) -> Result<()> {
249 if bytes.len() as u64 != u64::from(item.length) {
250 return Err(Error::SizeMismatch {
251 what,
252 expected: u64::from(item.length),
253 actual: bytes.len() as u64,
254 });
255 }
256 if Hash::of(bytes) != item.hash {
257 return Err(Error::HashMismatch { what });
258 }
259 Ok(())
260}
261
262pub(crate) fn lent(
268 store: &dyn Store,
269 kind: Stored,
270 hash: &Hash,
271 with: &mut dyn FnMut(&[u8]) -> Result<()>,
272) -> Result<bool> {
273 let mut calls = 0_u32;
274 let mut failed = None;
275 let held = store.lend(kind, hash, &mut |bytes| {
276 calls += 1;
277 let done = if calls == 1 {
278 with(bytes)
279 } else {
280 Err(Error::Invariant {
281 detail: "a store lent one thing twice",
282 })
283 };
284 done.map_err(|error| {
285 failed.get_or_insert(error);
286 Error::Invariant {
288 detail: "what a store lent was refused",
289 }
290 })
291 });
292 if let Some(error) = failed {
293 return Err(error);
294 }
295 match (held?, calls) {
296 (true, 1) => Ok(true),
297 (false, 0) => Ok(false),
298 (true, _) => Err(Error::Invariant {
299 detail: "a store said it held a thing, and lent nothing",
300 }),
301 (false, _) => Err(Error::Invariant {
302 detail: "a store lent a thing, and said it did not hold it",
303 }),
304 }
305}
306
307pub(crate) fn lend_chunk(
312 store: &dyn Store,
313 item: &ChunkRef,
314 with: &mut dyn FnMut(&[u8]) -> Result<()>,
315) -> Result<bool> {
316 lent(store, Stored::Chunk, &item.hash, &mut |bytes| {
317 check_stored_chunk(item, bytes)?;
318 with(bytes)
319 })
320}
321
322fn check_stored_chunk(item: &ChunkRef, bytes: &[u8]) -> Result<()> {
326 if Hash::of(bytes) != item.hash {
327 return Err(damaged("a stored chunk", &item.hash));
328 }
329 if bytes.len() as u64 != u64::from(item.length) {
330 return Err(Error::SizeMismatch {
331 what: "a stored chunk",
332 expected: u64::from(item.length),
333 actual: bytes.len() as u64,
334 });
335 }
336 Ok(())
337}
338
339pub(crate) fn missing(what: &'static str, hash: &Hash) -> Error {
340 Error::Missing { what, hash: *hash }
341}
342
343fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
344 mutex
345 .lock()
346 .unwrap_or_else(|poisoned| poisoned.into_inner())
347}
348
349const AHEAD_BYTES: u64 = 64 << 20;
351
352const AHEAD_COUNT: usize = 1024;
354
355const AHEAD_BYTES_BY_EACH: u64 = 1 << 20;
357
358#[derive(Clone, Copy)]
360enum Ahead {
361 Checked,
363 Framed,
366}
367
368type Given<'g> = dyn FnMut(usize, Option<&[u8]>) -> Result<()> + 'g;
371
372fn slot_length(item: &ChunkRef, ahead: Ahead) -> usize {
374 let length = item.length as usize;
375 match ahead {
376 Ahead::Checked => length,
377 Ahead::Framed => length + 1,
378 }
379}
380
381fn read_slot(store: &dyn Store, item: &ChunkRef, ahead: Ahead, slot: &mut [u8]) -> Result<bool> {
388 let length = u64::from(item.length);
389 match ahead {
390 Ahead::Checked => match store.read_into(Stored::Chunk, &item.hash, slot)? {
391 None => Ok(false),
392 Some(held) if held == length => {
393 check_stored_chunk(item, slot)?;
394 Ok(true)
395 }
396 Some(_) => match lend_chunk(store, item, &mut |_| Ok(()))? {
397 false => Ok(false),
398 true => Err(Error::Invariant {
399 detail: "a store gave two lengths for one chunk",
400 }),
401 },
402 },
403 Ahead::Framed => {
404 let Some((tag, body)) = slot.split_first_mut() else {
405 return Err(Error::Invariant {
406 detail: "a chunk's frame has no room for its tag",
407 });
408 };
409 *tag = FRAME_CHUNK;
410 Ok(store.read_into(Stored::Chunk, &item.hash, body)? == Some(length))
411 }
412 }
413}
414
415pub(crate) struct ChunksAhead {
422 memory: Vec<u8>,
423 threads: usize,
424}
425
426impl ChunksAhead {
427 pub(crate) fn new() -> Self {
428 Self::with_threads(0)
429 }
430
431 pub(crate) fn with_threads(threads: usize) -> Self {
434 let threads = match threads {
435 0 => std::thread::available_parallelism().map_or(1, usize::from),
436 threads => threads,
437 };
438 Self {
439 memory: Vec::new(),
440 threads,
441 }
442 }
443
444 pub(crate) fn lend(
452 &mut self,
453 store: &dyn Store,
454 items: &[ChunkRef],
455 with: &mut dyn FnMut(usize, &[u8]) -> Result<()>,
456 ) -> Result<()> {
457 self.each(
458 store,
459 items,
460 Ahead::Checked,
461 &mut |place, bytes| match bytes {
462 Some(bytes) => with(place, bytes),
463 None => Err(missing("the chunk", &items[place].hash)),
464 },
465 )
466 }
467
468 pub(crate) fn frames(
475 &mut self,
476 store: &dyn Store,
477 items: &[ChunkRef],
478 with: &mut dyn FnMut(Option<&[u8]>) -> Result<()>,
479 ) -> Result<()> {
480 self.each(store, items, Ahead::Framed, &mut |_, frame| with(frame))
481 }
482
483 fn each(
487 &mut self,
488 store: &dyn Store,
489 items: &[ChunkRef],
490 ahead: Ahead,
491 with: &mut Given<'_>,
492 ) -> Result<()> {
493 let mut first = 0;
494 while first < items.len() {
495 let mut count = 0;
496 let mut bytes = 0_u64;
497 for item in items[first..].iter().take(AHEAD_COUNT) {
498 if count > 0 && bytes + u64::from(item.length) > AHEAD_BYTES {
499 break;
500 }
501 bytes += u64::from(item.length);
502 count += 1;
503 }
504 let batch = &items[first..first + count];
505 let worth = usize::try_from(bytes / AHEAD_BYTES_BY_EACH).unwrap_or(usize::MAX);
506 let workers = self.threads.min(worth.saturating_add(1)).min(count);
507 let mut with_place = |index: usize, bytes: Option<&[u8]>| with(first + index, bytes);
508 let ended = if workers <= 1 {
509 self.one_at_a_time(store, batch, ahead, &mut with_place)?
510 } else {
511 self.several_at_once(store, batch, ahead, workers, &mut with_place)?
512 };
513 if ended {
514 return Ok(());
515 }
516 first += count;
517 }
518 Ok(())
519 }
520
521 fn one_at_a_time(
524 &mut self,
525 store: &dyn Store,
526 batch: &[ChunkRef],
527 ahead: Ahead,
528 with: &mut Given<'_>,
529 ) -> Result<bool> {
530 for (index, item) in batch.iter().enumerate() {
531 let held = match ahead {
532 Ahead::Checked => lend_chunk(store, item, &mut |bytes| with(index, Some(bytes)))?,
533 Ahead::Framed => {
534 let frame = self.room(slot_length(item, ahead));
535 let held = read_slot(store, item, ahead, frame)?;
536 if held {
537 with(index, Some(frame))?;
538 }
539 held
540 }
541 };
542 if !held {
543 with(index, None)?;
544 return Ok(true);
545 }
546 }
547 Ok(false)
548 }
549
550 fn several_at_once(
553 &mut self,
554 store: &dyn Store,
555 batch: &[ChunkRef],
556 ahead: Ahead,
557 workers: usize,
558 with: &mut Given<'_>,
559 ) -> Result<bool> {
560 let lengths: Vec<usize> = batch.iter().map(|item| slot_length(item, ahead)).collect();
561 let mut rest = self.room(lengths.iter().sum());
564 let slots: Vec<Mutex<&mut [u8]>> = lengths
565 .iter()
566 .map(|&length| {
567 let (slot, after) = std::mem::take(&mut rest).split_at_mut(length);
568 rest = after;
569 Mutex::new(slot)
570 })
571 .collect();
572 let next = AtomicUsize::new(0);
573 let work = || {
574 let mut done = Vec::new();
575 loop {
576 let index = next.fetch_add(1, Ordering::Relaxed);
577 let Some(item) = batch.get(index) else {
578 return done;
579 };
580 let held = read_slot(store, item, ahead, &mut lock(&slots[index]));
581 done.push((index, held));
582 }
583 };
584 let mut done: Vec<_> = on_threads(workers, &work).into_iter().flatten().collect();
585 done.sort_unstable_by_key(|(index, _)| *index);
586 for (index, held) in done {
587 if !held? {
588 with(index, None)?;
589 return Ok(true);
590 }
591 with(index, Some(&lock(&slots[index])))?;
592 }
593 Ok(false)
594 }
595
596 fn room(&mut self, length: usize) -> &mut [u8] {
600 if self.memory.len() < length {
601 self.memory = vec![0; length];
602 }
603 &mut self.memory[..length]
604 }
605
606 #[cfg(test)]
608 fn memory_held(&self) -> usize {
609 self.memory.capacity()
610 }
611}
612
613#[cfg(test)]
614mod tests {
615 use super::*;
616
617 #[test]
618 fn a_store_keeps_each_kind_apart_and_each_thing_once() {
619 let store = MemoryStore::new();
620 let hash = Hash::of(b"x");
621 store.put(Stored::Chunk, &hash, b"x").unwrap();
622 store.put(Stored::Chunk, &hash, b"x").unwrap();
623 assert_eq!(store.count(Stored::Chunk), 1);
624 assert_eq!(store.bytes(Stored::Chunk), 1);
625 assert!(store.has(Stored::Chunk, &hash).unwrap());
626 assert!(!store.has(Stored::FileList, &hash).unwrap());
627 assert_eq!(store.size(Stored::Chunk, &hash).unwrap(), Some(1));
628 assert_eq!(store.size(Stored::Record, &hash).unwrap(), None);
629 assert_eq!(&*store.get(Stored::Chunk, &hash).unwrap().unwrap(), b"x");
630 assert_eq!(store.hashes(Stored::Chunk), vec![hash]);
631 store.remove(Stored::Chunk, &hash).unwrap();
632 assert!(store.get(Stored::Chunk, &hash).unwrap().is_none());
633 }
634
635 struct Sized {
638 inner: MemoryStore,
639 read: std::sync::atomic::AtomicUsize,
640 }
641
642 impl Store for Sized {
643 fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
644 self.inner.put(kind, hash, bytes)
645 }
646
647 fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
648 self.read.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
649 self.inner.get(kind, hash)
650 }
651
652 fn size(&self, kind: Stored, hash: &Hash) -> Result<Option<u64>> {
653 self.inner.size(kind, hash)
654 }
655
656 fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
657 self.inner.remove(kind, hash)
658 }
659 }
660
661 #[test]
662 fn a_store_that_says_how_long_a_thing_is_is_not_read_to_see_what_it_holds() {
663 let store = Sized {
664 inner: MemoryStore::new(),
665 read: Default::default(),
666 };
667 let content: Vec<u8> = (0..5000_u32).map(|at| (at % 251) as u8).collect();
668 let mut builder = crate::Builder::new(&store, crate::Chunking::fixed(16).unwrap());
669 let first = builder.file_from_bytes(&content).unwrap();
670 let again = builder.file_from_bytes(&content).unwrap();
674 assert_eq!(again, first);
675 assert!(store.inner.count(Stored::Chunk) > 100);
676 assert_eq!(store.read.load(std::sync::atomic::Ordering::Relaxed), 0);
677 assert!(!store.has(Stored::Chunk, &Hash::of(b"not there")).unwrap());
679 assert!(store.has(Stored::FileList, &first.hash).unwrap());
680 assert!(!store.has(Stored::FolderList, &first.hash).unwrap());
681 }
682
683 #[test]
684 fn what_is_read_from_a_store_is_checked_before_it_is_used() {
685 let store = MemoryStore::new();
686 let item = ChunkRef::of(b"right").unwrap();
687 let seen = std::cell::Cell::new(0);
689 let mut count = |_: &[u8]| {
690 seen.set(seen.get() + 1);
691 Ok(())
692 };
693 store.put(Stored::Chunk, &item.hash, b"wrong").unwrap();
695 assert!(matches!(
696 lend_chunk(&store, &item, &mut count),
697 Err(Error::Corrupt { .. })
698 ));
699 store.remove(Stored::Chunk, &item.hash).unwrap();
700 store.put(Stored::Chunk, &item.hash, b"longer").unwrap();
701 assert!(matches!(
702 lend_chunk(&store, &item, &mut count),
703 Err(Error::Corrupt { .. })
704 ));
705 store.remove(Stored::Chunk, &item.hash).unwrap();
707 store.put(Stored::Chunk, &item.hash, b"right").unwrap();
708 assert!(matches!(
709 lend_chunk(&store, &ChunkRef { length: 4, ..item }, &mut count),
710 Err(Error::SizeMismatch { .. })
711 ));
712 assert_eq!(seen.get(), 0);
714 assert!(matches!(lend_chunk(&store, &item, &mut count), Ok(true)));
715 assert_eq!(seen.get(), 1);
716
717 let list = FileList::new(vec![item]).unwrap();
719 let other = FileList::new(vec![item, item]).unwrap();
720 store
721 .put(Stored::FileList, list.hash(), &other.encode())
722 .unwrap();
723 assert!(matches!(
724 load_file(&store, &list.target()),
725 Err(Error::Corrupt { .. })
726 ));
727 store
729 .put(Stored::FileList, other.hash(), &other.encode())
730 .unwrap();
731 let resized = Target {
732 size: other.size() + 1,
733 ..other.target()
734 };
735 assert!(matches!(
736 load_file(&store, &resized),
737 Err(Error::SizeMismatch { .. })
738 ));
739 assert!(load_file(&store, &other.target()).unwrap().is_some());
740 let as_folder = Target {
742 kind: Kind::Folder,
743 ..other.target()
744 };
745 assert!(load_folder(&store, &as_folder).unwrap().is_none());
746 assert!(load_file(&store, &as_folder).is_err());
747
748 let root = Root::of_record(b"one");
750 store.put(Stored::Record, root.as_hash(), b"two").unwrap();
751 assert!(matches!(
752 load_record(&store, &root),
753 Err(Error::Corrupt { .. })
754 ));
755 }
756
757 struct Lending {
759 inner: MemoryStore,
760 calls: usize,
762 says: bool,
764 passes_over: bool,
766 }
767
768 impl Store for Lending {
769 fn put(&self, kind: Stored, hash: &Hash, bytes: &[u8]) -> Result<()> {
770 self.inner.put(kind, hash, bytes)
771 }
772
773 fn get(&self, kind: Stored, hash: &Hash) -> Result<Option<Arc<[u8]>>> {
774 self.inner.get(kind, hash)
775 }
776
777 fn lend(
778 &self,
779 kind: Stored,
780 hash: &Hash,
781 with: &mut dyn FnMut(&[u8]) -> Result<()>,
782 ) -> Result<bool> {
783 let bytes = self
784 .inner
785 .get(kind, hash)?
786 .unwrap_or_else(|| Arc::from(&b""[..]));
787 for _ in 0..self.calls {
788 let done = with(&bytes);
789 if !self.passes_over {
790 done?;
791 }
792 }
793 Ok(self.says)
794 }
795
796 fn remove(&self, kind: Stored, hash: &Hash) -> Result<()> {
797 self.inner.remove(kind, hash)
798 }
799 }
800
801 #[test]
802 fn a_store_is_held_to_what_lending_promises() {
803 let item = ChunkRef::of(b"right").unwrap();
804 let lend = |calls, says, passes_over, kept: &[u8]| {
806 let store = Lending {
807 inner: MemoryStore::new(),
808 calls,
809 says,
810 passes_over,
811 };
812 store.put(Stored::Chunk, &item.hash, kept).unwrap();
813 let mut seen = Vec::new();
814 let lent = lend_chunk(&store, &item, &mut |bytes| {
815 seen.push(bytes.to_vec());
816 Ok(())
817 });
818 (lent, seen)
819 };
820 let (lent, seen) = lend(1, true, false, b"right");
822 assert!(matches!(lent, Ok(true)));
823 assert_eq!(seen, vec![b"right".to_vec()]);
824 assert!(matches!(lend(0, false, false, b"right").0, Ok(false)));
825 let (lent, seen) = lend(2, true, true, b"right");
827 assert!(matches!(lent, Err(Error::Invariant { .. })));
828 assert_eq!(seen.len(), 1);
829 assert!(matches!(
831 lend(0, true, false, b"right").0,
832 Err(Error::Invariant { .. })
833 ));
834 assert!(matches!(
835 lend(1, false, false, b"right").0,
836 Err(Error::Invariant { .. })
837 ));
838 for passes_over in [false, true] {
841 let (lent, seen) = lend(1, true, passes_over, b"wrong");
842 assert!(matches!(lent, Err(Error::Corrupt { .. })));
843 assert!(seen.is_empty());
844 }
845 }
846
847 #[test]
848 fn chunks_read_ahead_take_no_more_memory_than_the_longest_batch() {
849 let store = MemoryStore::new();
853 let long_bytes = vec![7_u8; 1 << 20];
854 let long = ChunkRef::of(&long_bytes).unwrap();
855 store.put(Stored::Chunk, &long.hash, &long_bytes).unwrap();
856 let short = ChunkRef::of(b"s").unwrap();
857 store.put(Stored::Chunk, &short.hash, b"s").unwrap();
858 let mut ahead = ChunksAhead::with_threads(4);
859 for place in 0..64 {
860 let mut items = vec![short; place];
861 items.push(long);
862 let mut given = Vec::new();
863 ahead
864 .frames(&store, &items, &mut |frame| {
865 given.push(frame.map(<[u8]>::len));
866 Ok(())
867 })
868 .unwrap();
869 let mut lengths: Vec<_> = items
870 .iter()
871 .map(|item| Some(item.length as usize + 1))
872 .collect();
873 assert_eq!(given, lengths);
874 if place % 8 == 0 {
876 given.clear();
877 ahead
878 .lend(&store, &items, &mut |_, bytes| {
879 given.push(Some(bytes.len()));
880 Ok(())
881 })
882 .unwrap();
883 lengths
884 .iter_mut()
885 .for_each(|length| *length = length.map(|length| length - 1));
886 assert_eq!(given, lengths);
887 }
888 }
889 let most = long_bytes.len() + 1 + 64 * 2;
890 assert!(ahead.memory_held() <= most, "{} bytes", ahead.memory_held());
891 }
892}