// Takes chunks extracted from data, and creates pages out of them, by mapping
// them to the corresponding page, leaving uninitialized parts filled with
// zeros.
Takes chunks extracted from data, and creates pages out of them, by mapping
them to the corresponding page, leaving uninitialized parts filled with
zeros.
Takes chunks extracted from data and creates pages out of them by mapping them to the corresponding page leaving uninitialized parts filled with zeros .
pub fn as_pages(self) -> Vec<(PageIndex, PageBytes)> {
        self.0
            .iter()
            // We go over all chunks and split them into multiple chunks if they cross page
            // boundaries.
            .flat_map(|(offset, bytes)| {
                // First, we determine the size of the first chunk, which is equal to the chunk
                // itself, if it does not cross the page boundary.
                let first_chunk_size = std::cmp::min(bytes.len(), PAGE_SIZE - (offset % PAGE_SIZE));
                let mut split_chunks = vec![(*offset, bytes[..first_chunk_size].to_vec())];
                // If the chunk crosses the page boundary, split the rest of it into
                // page-sized chunks and compute the correct offset for them.
                split_chunks.extend_from_slice(
                    bytes[first_chunk_size..]
                        .chunks(PAGE_SIZE)
                        .enumerate()
                        .map(move |(chunk_num, chunk)| {
                            (
                                offset + first_chunk_size + PAGE_SIZE * chunk_num,
                                chunk.to_vec(),
                            )
                        })
                        .collect::<Vec<(usize, Vec<u8>)>>()
                        .as_slice(),
                );
                split_chunks
            })
            // Second, after we know, that no chunk crosses the page boundary, simply fold all of
            // them into a map page_num -> page. Whenever we map a chunk into its page,
            // we simply copy its bytes to the right place inside the page.
            .fold(HashMap::new(), |mut acc, (offset, bytes)| {
                let page_num = offset / PAGE_SIZE;
                let list = acc
                    .entry(PageIndex::new(page_num as u64))
                    .or_insert_with(|| [0; PAGE_SIZE]);
                let local_offset = offset % PAGE_SIZE;
                list[local_offset..local_offset + (bytes.len() as usize)].copy_from_slice(&bytes);
                acc
            })
            .into_iter()
            .collect()
    }
pub fn as_pages ( self ) -> Vec < ( PageIndex , PageBytes ) > { self . 0 . iter ( ) // We go over all chunks and split them into multiple chunks if they cross page // boundaries. . flat_map ( | ( offset , bytes ) | { // First, we determine the size of the first chunk, which is equal to the chunk // itself, if it does not cross the page boundary. let first_chunk_size = std :: cmp :: min ( bytes . len ( ) , PAGE_SIZE - ( offset % PAGE_SIZE ) ) ; let mut split_chunks = vec ! [ ( offset bytes [ first_chunk_size ] to_vec ( ) ) ] ; // If the chunk crosses the page boundary, split the rest of it into // page-sized chunks and compute the correct offset for them. split_chunks . extend_from_slice ( bytes [ first_chunk_size .. ] . chunks ( PAGE_SIZE ) . enumerate ( ) . map ( move | ( chunk_num , chunk ) | { ( offset + first_chunk_size + PAGE_SIZE * chunk_num , chunk . to_vec ( ) , ) } ) . collect :: < Vec < ( usize , Vec < u8 > ) > > ( ) . as_slice ( ) , ) ; split_chunks } ) // Second, after we know, that no chunk crosses the page boundary, simply fold all of // them into a map page_num -> page. Whenever we map a chunk into its page, // we simply copy its bytes to the right place inside the page. . fold ( HashMap :: new ( ) , | mut acc , ( offset , bytes ) | { let page_num = offset / PAGE_SIZE ; let list = acc . entry ( PageIndex :: new ( page_num as u64 ) ) . or_insert_with ( | | [ 0 ; PAGE_SIZE ] ) ; let local_offset = offset % PAGE_SIZE ; list [ local_offset .. local_offset + ( bytes . len ( ) as usize ) ] . copy_from_slice ( & bytes ) ; acc } ) . into_iter ( ) . collect ( ) }
Takes chunks extracted from data, and creates pages out of them, by mapping
them to the corresponding page, leaving uninitialized parts filled with
zeros.
Takes chunks extracted from data and creates pages out of them by mapping them to the corresponding page leaving uninitialized parts filled with zeros .
self