fn from(data: &'static [u8]) -> Result<PythonModulesData, &'static str> {
        let mut reader = Cursor::new(data);

        let count = reader
            .read_u32::<LittleEndian>()
            .or_else(|_| Err("failed reading count"))?;

        let mut index = Vec::with_capacity(count as usize);
        let mut total_names_length = 0;
        let mut total_sources_length = 0;
        let mut package_count = 0;

        for _ in 0..count {
            let name_length = reader
                .read_u32::<LittleEndian>()
                .or_else(|_| Err("failed reading name length"))?
                as usize;
            let source_length = reader
                .read_u32::<LittleEndian>()
                .or_else(|_| Err("failed reading source length"))?
                as usize;
            let bytecode_length = reader
                .read_u32::<LittleEndian>()
                .or_else(|_| Err("failed reading bytecode length"))?
                as usize;
            let flags = reader
                .read_u32::<LittleEndian>()
                .or_else(|_| Err("failed reading module flags"))?;

            let is_package = flags & 0x01 != 0;

            if is_package {
                package_count += 1;
            }

            index.push((name_length, source_length, bytecode_length, is_package));
            total_names_length += name_length;
            total_sources_length += source_length;
        }

        let mut res = HashMap::with_capacity(count as usize);
        let mut packages = HashSet::with_capacity(package_count);
        let sources_start_offset = reader.position() as usize + total_names_length;
        let bytecodes_start_offset = sources_start_offset + total_sources_length;

        let mut sources_current_offset: usize = 0;
        let mut bytecodes_current_offset: usize = 0;

        for (name_length, source_length, bytecode_length, is_package) in index {
            let offset = reader.position() as usize;

            let name =
                unsafe { std::str::from_utf8_unchecked(&data[offset..offset + name_length]) };

            let source_offset = sources_start_offset + sources_current_offset;
            let source = if source_length > 0 {
                Some(&data[source_offset..source_offset + source_length])
            } else {
                None
            };

            let bytecode_offset = bytecodes_start_offset + bytecodes_current_offset;
            let bytecode = if bytecode_length > 0 {
                Some(&data[bytecode_offset..bytecode_offset + bytecode_length])
            } else {
                None
            };

            reader.set_position(offset as u64 + name_length as u64);

            sources_current_offset += source_length;
            bytecodes_current_offset += bytecode_length;

            if is_package {
                packages.insert(name);
            }

            // Extension modules will have their names present to populate the
            // packages set. So only populate module data if we have data for it.
            if source.is_some() || bytecode.is_some() {
                res.insert(name, PythonModuleData { source, bytecode });
            }
        }

        Ok(PythonModulesData {
            packages,
            data: res,
        })
    }
/// Construct a new instance from a memory slice.
Construct a new instance from a memory slice.
Construct a new instance from a memory slice .
fn from(data: &'static [u8]) -> Result<PythonModulesData, &'static str> {
        let mut reader = Cursor::new(data);
        let count = reader
            .read_u32::<LittleEndian>()
            .or_else(|_| Err("failed reading count"))?;
        let mut index = Vec::with_capacity(count as usize);
        let mut total_names_length = 0;
        let mut total_sources_length = 0;
        let mut package_count = 0;
        for _ in 0..count {
            let name_length = reader
                .read_u32::<LittleEndian>()
                .or_else(|_| Err("failed reading name length"))?
                as usize;
            let source_length = reader
                .read_u32::<LittleEndian>()
                .or_else(|_| Err("failed reading source length"))?
                as usize;
            let bytecode_length = reader
                .read_u32::<LittleEndian>()
                .or_else(|_| Err("failed reading bytecode length"))?
                as usize;
            let flags = reader
                .read_u32::<LittleEndian>()
                .or_else(|_| Err("failed reading module flags"))?;
            let is_package = flags & 0x01 != 0;
            if is_package {
                package_count += 1;
            }
            index.push((name_length, source_length, bytecode_length, is_package));
            total_names_length += name_length;
            total_sources_length += source_length;
        }
        let mut res = HashMap::with_capacity(count as usize);
        let mut packages = HashSet::with_capacity(package_count);
        let sources_start_offset = reader.position() as usize + total_names_length;
        let bytecodes_start_offset = sources_start_offset + total_sources_length;
        let mut sources_current_offset: usize = 0;
        let mut bytecodes_current_offset: usize = 0;
        for (name_length, source_length, bytecode_length, is_package) in index {
            let offset = reader.position() as usize;
            let name =
                unsafe { std::str::from_utf8_unchecked(&data[offset..offset + name_length]) };
            let source_offset = sources_start_offset + sources_current_offset;
            let source = if source_length > 0 {
                Some(&data[source_offset..source_offset + source_length])
            } else {
                None
            };
            let bytecode_offset = bytecodes_start_offset + bytecodes_current_offset;
            let bytecode = if bytecode_length > 0 {
                Some(&data[bytecode_offset..bytecode_offset + bytecode_length])
            } else {
                None
            };
            reader.set_position(offset as u64 + name_length as u64);
            sources_current_offset += source_length;
            bytecodes_current_offset += bytecode_length;
            if is_package {
                packages.insert(name);
            }
            // Extension modules will have their names present to populate the
            // packages set. So only populate module data if we have data for it.
            if source.is_some() || bytecode.is_some() {
                res.insert(name, PythonModuleData { source, bytecode });
            }
        }
        Ok(PythonModulesData {
            packages,
            data: res,
        })
    }
fn from ( data : & ' static [ u8 ] ) -> Result < PythonModulesData , & ' static str > { let mut reader = Cursor :: new ( data ) ; let count = reader . read_u32 :: < LittleEndian > ( ) . or_else ( | _ | Err ( " " ) ) ? ; let mut index = Vec :: with_capacity ( count as usize ) ; let mut total_names_length = 0 ; let mut total_sources_length = 0 ; let mut package_count = 0 ; for _ in 0 .. count { let name_length = reader . read_u32 :: < LittleEndian > ( ) . or_else ( | _ | Err ( " " ) ) ? as usize ; let source_length = reader . read_u32 :: < LittleEndian > ( ) . or_else ( | _ | Err ( " " ) ) ? as usize ; let bytecode_length = reader . read_u32 :: < LittleEndian > ( ) . or_else ( | _ | Err ( " " ) ) ? as usize ; let flags = reader . read_u32 :: < LittleEndian > ( ) . or_else ( | _ | Err ( " " ) ) ? ; let is_package = flags & 0x01 != 0 ; if is_package { package_count += 1 ; } index . push ( ( name_length , source_length , bytecode_length , is_package ) ) ; total_names_length += name_length ; total_sources_length += source_length ; } let mut res = HashMap :: with_capacity ( count as usize ) ; let mut packages = HashSet :: with_capacity ( package_count ) ; let sources_start_offset = reader . position ( ) as usize + total_names_length ; let bytecodes_start_offset = sources_start_offset + total_sources_length ; let mut sources_current_offset : usize = 0 ; let mut bytecodes_current_offset : usize = 0 ; for ( name_length , source_length , bytecode_length , is_package ) in index { let offset = reader . position ( ) as usize ; let name = unsafe { std :: str :: from_utf8_unchecked ( & data [ offset .. offset + name_length ] ) } ; let source_offset = sources_start_offset + sources_current_offset ; let source = if source_length > 0 { Some ( & data [ source_offset .. source_offset + source_length ] ) } else { None } ; let bytecode_offset = bytecodes_start_offset + bytecodes_current_offset ; let bytecode = if bytecode_length > 0 { Some ( & data [ bytecode_offset .. bytecode_offset + bytecode_length ] ) } else { None } ; reader . set_position ( offset as u64 + name_length as u64 ) ; sources_current_offset += source_length ; bytecodes_current_offset += bytecode_length ; if is_package { packages . insert ( name ) ; } // Extension modules will have their names present to populate the // packages set. So only populate module data if we have data for it. if source . is_some ( ) || bytecode . is_some ( ) { res . insert ( name , PythonModuleData { source , bytecode } ) ; } } Ok ( PythonModulesData { packages , data : res , } ) }
Construct a new instance from a memory slice.
Construct a new instance from a memory slice .
data
&'static [u8]