pub fn store(&mut self, address: u64, value: V) -> Result<()> {
        if value.bits() % 8 != 0 || value.bits() == 0 {
            return Err(format!(
                "Storing value in paged memory with bit width not divisible by 8 and > 0 {}",
                value.bits()
            )
            .into());
        }

        // There are a few scenarios here we need to account for
        // E is for Expression, B is for Backref. Consider a 4-byte write, with
        // the original memory on top and our write immediately underneath that.
        //
        // Case 0
        // EEEBEE   The easiest scenario, we just replace expressions in place
        //  WWWW
        //
        // Case 1
        // EBBEEE   We overwrite some backrefs that refer to before our write.
        //  WWWW    We need to truncate the expression before.
        //          First byte we overwrite is a backref.
        //
        // Case 2
        // EEEBBB   We overwrite an expression that starts in the middle of our
        //  WWWW    write.
        //          Byte after last byte is a backref.
        //
        // Case 3
        // EBBBBB   We overwrite an expression that starts before our expression,
        //  WWWW    and continues after out expression.
        //          Handle case 2, then case 1, and case 3 will be fine.

        // Handle backrefs that come after by finding the first address after
        // our write, truncating it to the appropriate size, and rewriting it
        let address_after_write = address + (value.bits() / 8) as u64;

        let value_to_write = if let Some(MemoryCell::Backref(backref_address)) =
            self.load_cell(address_after_write)
        {
            let backref_value = self
                .load_cell(*backref_address)
                .ok_or("Backref cell pointed to null cell")?
                .value()
                .ok_or("Backref cell pointed to cell without value")?;
            // furthest most address backref value reaches
            let backref_furthest_address = backref_address + (backref_value.bits() / 8) as u64;
            // how many bits are left after our write
            let left_bits = ((backref_furthest_address - address_after_write) * 8) as usize;
            // load that value
            self.load(address_after_write, left_bits)?
        } else {
            None
        };

        if let Some(value_to_write) = value_to_write {
            self.store_no_backref(address_after_write, value_to_write);
        }

        // handle values we overwrite before this write
        let value_to_write =
            if let Some(MemoryCell::Backref(backref_address)) = self.load_cell(address) {
                let backref_value = self.load_cell(*backref_address).unwrap().value().unwrap();
                // furthest most address backref value reaches
                let backref_furthest_address = backref_address + (backref_value.bits() / 8) as u64;
                // how many bits are we about to overwrite
                let overwrite_bits = (backref_furthest_address - address) * 8;
                // how many bits are left over
                let left_bits = backref_value.bits() - overwrite_bits as usize;
                Some((*backref_address, self.load(*backref_address, left_bits)?))
            } else {
                None
            };

        if let Some(value_to_write) = value_to_write {
            self.store_no_backref(value_to_write.0, value_to_write.1.unwrap());
        }

        // Go ahead and store this value
        self.store_no_backref(address, value);

        Ok(())
    }
/// Store a value at the given address.
///
/// The value must have a bit-width >= 8, and the bit-width must be evenly
/// divisible by 8.
Store a value at the given address.
The value must have a bit-width >= 8, and the bit-width must be evenly
divisible by 8.
Store a value at the given address . The value must have a bit - width > = 8 and the bit - width must be evenly divisible by 8 .
pub fn store(&mut self, address: u64, value: V) -> Result<()> {
        if value.bits() % 8 != 0 || value.bits() == 0 {
            return Err(format!(
                "Storing value in paged memory with bit width not divisible by 8 and > 0 {}",
                value.bits()
            )
            .into());
        }
        // There are a few scenarios here we need to account for
        // E is for Expression, B is for Backref. Consider a 4-byte write, with
        // the original memory on top and our write immediately underneath that.
        //
        // Case 0
        // EEEBEE   The easiest scenario, we just replace expressions in place
        //  WWWW
        //
        // Case 1
        // EBBEEE   We overwrite some backrefs that refer to before our write.
        //  WWWW    We need to truncate the expression before.
        //          First byte we overwrite is a backref.
        //
        // Case 2
        // EEEBBB   We overwrite an expression that starts in the middle of our
        //  WWWW    write.
        //          Byte after last byte is a backref.
        //
        // Case 3
        // EBBBBB   We overwrite an expression that starts before our expression,
        //  WWWW    and continues after out expression.
        //          Handle case 2, then case 1, and case 3 will be fine.
        // Handle backrefs that come after by finding the first address after
        // our write, truncating it to the appropriate size, and rewriting it
        let address_after_write = address + (value.bits() / 8) as u64;
        let value_to_write = if let Some(MemoryCell::Backref(backref_address)) =
            self.load_cell(address_after_write)
        {
            let backref_value = self
                .load_cell(*backref_address)
                .ok_or("Backref cell pointed to null cell")?
                .value()
                .ok_or("Backref cell pointed to cell without value")?;
            // furthest most address backref value reaches
            let backref_furthest_address = backref_address + (backref_value.bits() / 8) as u64;
            // how many bits are left after our write
            let left_bits = ((backref_furthest_address - address_after_write) * 8) as usize;
            // load that value
            self.load(address_after_write, left_bits)?
        } else {
            None
        };
        if let Some(value_to_write) = value_to_write {
            self.store_no_backref(address_after_write, value_to_write);
        }
        // handle values we overwrite before this write
        let value_to_write =
            if let Some(MemoryCell::Backref(backref_address)) = self.load_cell(address) {
                let backref_value = self.load_cell(*backref_address).unwrap().value().unwrap();
                // furthest most address backref value reaches
                let backref_furthest_address = backref_address + (backref_value.bits() / 8) as u64;
                // how many bits are we about to overwrite
                let overwrite_bits = (backref_furthest_address - address) * 8;
                // how many bits are left over
                let left_bits = backref_value.bits() - overwrite_bits as usize;
                Some((*backref_address, self.load(*backref_address, left_bits)?))
            } else {
                None
            };
        if let Some(value_to_write) = value_to_write {
            self.store_no_backref(value_to_write.0, value_to_write.1.unwrap());
        }
        // Go ahead and store this value
        self.store_no_backref(address, value);
        Ok(())
    }
pub fn store ( & mut self , address : u64 , value : V ) -> Result < ( ) > { if value . bits ( ) % 8 != 0 || value . bits ( ) == 0 { return Err ( format ! ( " " value bits ( ) ) . into ( ) ) ; } // There are a few scenarios here we need to account for // E is for Expression, B is for Backref. Consider a 4-byte write, with // the original memory on top and our write immediately underneath that. // // Case 0 // EEEBEE   The easiest scenario, we just replace expressions in place //  WWWW // // Case 1 // EBBEEE   We overwrite some backrefs that refer to before our write. //  WWWW    We need to truncate the expression before. //          First byte we overwrite is a backref. // // Case 2 // EEEBBB   We overwrite an expression that starts in the middle of our //  WWWW    write. //          Byte after last byte is a backref. // // Case 3 // EBBBBB   We overwrite an expression that starts before our expression, //  WWWW    and continues after out expression. //          Handle case 2, then case 1, and case 3 will be fine. // Handle backrefs that come after by finding the first address after // our write, truncating it to the appropriate size, and rewriting it let address_after_write = address + ( value . bits ( ) / 8 ) as u64 ; let value_to_write = if let Some ( MemoryCell :: Backref ( backref_address ) ) = self . load_cell ( address_after_write ) { let backref_value = self . load_cell ( * backref_address ) . ok_or ( " " ) ? . value ( ) . ok_or ( " " ) ? ; // furthest most address backref value reaches let backref_furthest_address = backref_address + ( backref_value . bits ( ) / 8 ) as u64 ; // how many bits are left after our write let left_bits = ( ( backref_furthest_address - address_after_write ) * 8 ) as usize ; // load that value self . load ( address_after_write , left_bits ) ? } else { None } ; if let Some ( value_to_write ) = value_to_write { self . store_no_backref ( address_after_write , value_to_write ) ; } // handle values we overwrite before this write let value_to_write = if let Some ( MemoryCell :: Backref ( backref_address ) ) = self . load_cell ( address ) { let backref_value = self . load_cell ( * backref_address ) . unwrap ( ) . value ( ) . unwrap ( ) ; // furthest most address backref value reaches let backref_furthest_address = backref_address + ( backref_value . bits ( ) / 8 ) as u64 ; // how many bits are we about to overwrite let overwrite_bits = ( backref_furthest_address - address ) * 8 ; // how many bits are left over let left_bits = backref_value . bits ( ) - overwrite_bits as usize ; Some ( ( * backref_address , self . load ( * backref_address , left_bits ) ? ) ) } else { None } ; if let Some ( value_to_write ) = value_to_write { self . store_no_backref ( value_to_write . 0 , value_to_write . 1 . unwrap ( ) ) ; } // Go ahead and store this value self . store_no_backref ( address , value ) ; Ok ( ( ) ) }
Store a value at the given address.
Store a value at the given address .
&mut self
address
u64
value
V
&mut self
address
u64
value
V