pub fn write(
        &self,
        src: &mut dyn std::io::Read,
        base: GuestAddress,
        offset: u64,
        count: u64,
    ) -> Result<()> {
        self.check_valid_offset(offset, count).chain_err(|| {
            format!(
                "Invalid offset: offset 0x{:X}, data length 0x{:X}, region size 0x{:X}",
                offset,
                count,
                self.size()
            )
        })?;

        match self.region_type {
            RegionType::Ram | RegionType::RamDevice => {
                let host_addr = self.mem_mapping.as_ref().unwrap().host_address();
                let slice = unsafe {
                    std::slice::from_raw_parts_mut((host_addr + offset) as *mut u8, count as usize)
                };
                src.read_exact(slice)
                    .chain_err(|| "Failed to write buffer to Ram")?;
            }
            RegionType::RomDevice | RegionType::IO => {
                if count >= std::usize::MAX as u64 {
                    return Err(ErrorKind::Overflow(count).into());
                }
                let mut slice = vec![0_u8; count as usize];
                src.read_exact(&mut slice).chain_err(|| {
                    "Failed to write buffer to slice, which will be provided for device"
                })?;

                let write_ops = self.ops.as_ref().unwrap().write.as_ref();
                if !write_ops(&slice, base, offset) {
                    return Err(ErrorKind::IoAccess(base.raw_value(), offset, count).into());
                }
            }
            _ => {
                return Err(ErrorKind::RegionType(self.region_type()).into());
            }
        }
        Ok(())
    }
/// Write data segment from `src` to memory.
///
/// # Arguments
///
/// * `src` - Source data.
/// * `base` - Base address.
/// * `offset` - Offset from base address.
/// * `count` - Size of data.
///
/// # Errors
///
/// Return Error if
/// * fail to access io region.
/// * the region is a container.
/// * the address overflows.
Write data segment from `src` to memory.
Write data segment from ` src ` to memory .
pub fn write(
        &self,
        src: &mut dyn std::io::Read,
        base: GuestAddress,
        offset: u64,
        count: u64,
    ) -> Result<()> {
        self.check_valid_offset(offset, count).chain_err(|| {
            format!(
                "Invalid offset: offset 0x{:X}, data length 0x{:X}, region size 0x{:X}",
                offset,
                count,
                self.size()
            )
        })?;
        match self.region_type {
            RegionType::Ram | RegionType::RamDevice => {
                let host_addr = self.mem_mapping.as_ref().unwrap().host_address();
                let slice = unsafe {
                    std::slice::from_raw_parts_mut((host_addr + offset) as *mut u8, count as usize)
                };
                src.read_exact(slice)
                    .chain_err(|| "Failed to write buffer to Ram")?;
            }
            RegionType::RomDevice | RegionType::IO => {
                if count >= std::usize::MAX as u64 {
                    return Err(ErrorKind::Overflow(count).into());
                }
                let mut slice = vec![0_u8; count as usize];
                src.read_exact(&mut slice).chain_err(|| {
                    "Failed to write buffer to slice, which will be provided for device"
                })?;
                let write_ops = self.ops.as_ref().unwrap().write.as_ref();
                if !write_ops(&slice, base, offset) {
                    return Err(ErrorKind::IoAccess(base.raw_value(), offset, count).into());
                }
            }
            _ => {
                return Err(ErrorKind::RegionType(self.region_type()).into());
            }
        }
        Ok(())
    }
pub fn write ( & self , src : & mut dyn std :: io :: Read , base : GuestAddress , offset : u64 , count : u64 , ) -> Result < ( ) > { self . check_valid_offset ( offset , count ) . chain_err ( | | { format ! ( " " offset count self size ( ) ) } ) ? ; match self . region_type { RegionType :: Ram | RegionType :: RamDevice => { let host_addr = self . mem_mapping . as_ref ( ) . unwrap ( ) . host_address ( ) ; let slice = unsafe { std :: slice :: from_raw_parts_mut ( ( host_addr + offset ) as * mut u8 , count as usize ) } ; src . read_exact ( slice ) . chain_err ( | | " " ) ? ; } RegionType :: RomDevice | RegionType :: IO => { if count >= std :: usize :: MAX as u64 { return Err ( ErrorKind :: Overflow ( count ) . into ( ) ) ; } let mut slice = vec ! [ 0_u8 count as usize ] ; src . read_exact ( & mut slice ) . chain_err ( | | { " " } ) ? ; let write_ops = self . ops . as_ref ( ) . unwrap ( ) . write . as_ref ( ) ; if ! write_ops ( & slice , base , offset ) { return Err ( ErrorKind :: IoAccess ( base . raw_value ( ) , offset , count ) . into ( ) ) ; } } _ => { return Err ( ErrorKind :: RegionType ( self . region_type ( ) ) . into ( ) ) ; } } Ok ( ( ) ) }
Write data segment from `src` to memory.
Write data segment from ` src ` to memory .
&self
src
&mut dyn std::io::Read
base
GuestAddress
offset
u64
count
u64
&self
src
&mut dyn std::io::Read
base
GuestAddress
offset
u64
count
u64
errors
Return Error if
fail to access io region.
the region is a container.
the address overflows.
Return Error if fail to access io region . the region is a container . the address overflows .