pub fn write(data: &mut [u8], record: &Record) -> Result<usize, ProgramError> {
        let offset = if let Some(idx) = record.index {
            // if the index was specified we will update existing record
            RefDB::get_offset(data, idx as usize)?
        } else {
            // otherwise either find a record with the supplied name or first available slot
            RefDB::find_write_offset(data, &record.name)?
        };
        let (cur_counter, cur_name) = RefDB::get_record_counter_and_name(data, offset)?;
        if cur_counter > 0 {
            // if the counter was specified we check that value is equal to stored,
            // to make sure there were no intermediate updates
            if record.counter > 0 && cur_counter != record.counter as usize {
                return Err(ProgramError::Custom(409));
            }
            // check that we are either writing to an empty slot or record name matches
            if record.index.is_some() && record.name != cur_name {
                return Err(ProgramError::Custom(409));
            }
        }
        // check that reference data type matches storage
        if RefDB::get_reference_type(data)? != record.reference.get_type() {
            return Err(ProgramError::Custom(409));
        }
        // update storage counters
        let storage_counter = RefDB::get_storage_counter(data)?;
        if (storage_counter as u32) < u32::MAX {
            RefDB::set_storage_counter(data, storage_counter + 1)?;
        } else {
            RefDB::set_storage_counter(data, 1)?;
        }
        if cur_counter == 0 {
            let active_records = RefDB::get_active_records(data)?;
            if (active_records as u32) < u32::MAX {
                RefDB::set_active_records(data, active_records + 1)?;
            }
        }
        // write record
        let res = record.pack(&mut data[offset..]);
        // update record counter
        if (cur_counter as u16) < u16::MAX {
            RefDB::set_record_counter(data, offset, cur_counter + 1);
        } else {
            RefDB::set_record_counter(data, offset, 1);
        }
        res
    }
/// Writes the record to the storage.
/// Uses the index if provided or searches the record by name.
/// If counter is provided it must be equal to stored value or error is returned.
Writes the record to the storage.
Uses the index if provided or searches the record by name.
If counter is provided it must be equal to stored value or error is returned.
Writes the record to the storage . Uses the index if provided or searches the record by name . If counter is provided it must be equal to stored value or error is returned .
pub fn write(data: &mut [u8], record: &Record) -> Result<usize, ProgramError> {
        let offset = if let Some(idx) = record.index {
            // if the index was specified we will update existing record
            RefDB::get_offset(data, idx as usize)?
        } else {
            // otherwise either find a record with the supplied name or first available slot
            RefDB::find_write_offset(data, &record.name)?
        };
        let (cur_counter, cur_name) = RefDB::get_record_counter_and_name(data, offset)?;
        if cur_counter > 0 {
            // if the counter was specified we check that value is equal to stored,
            // to make sure there were no intermediate updates
            if record.counter > 0 && cur_counter != record.counter as usize {
                return Err(ProgramError::Custom(409));
            }
            // check that we are either writing to an empty slot or record name matches
            if record.index.is_some() && record.name != cur_name {
                return Err(ProgramError::Custom(409));
            }
        }
        // check that reference data type matches storage
        if RefDB::get_reference_type(data)? != record.reference.get_type() {
            return Err(ProgramError::Custom(409));
        }
        // update storage counters
        let storage_counter = RefDB::get_storage_counter(data)?;
        if (storage_counter as u32) < u32::MAX {
            RefDB::set_storage_counter(data, storage_counter + 1)?;
        } else {
            RefDB::set_storage_counter(data, 1)?;
        }
        if cur_counter == 0 {
            let active_records = RefDB::get_active_records(data)?;
            if (active_records as u32) < u32::MAX {
                RefDB::set_active_records(data, active_records + 1)?;
            }
        }
        // write record
        let res = record.pack(&mut data[offset..]);
        // update record counter
        if (cur_counter as u16) < u16::MAX {
            RefDB::set_record_counter(data, offset, cur_counter + 1);
        } else {
            RefDB::set_record_counter(data, offset, 1);
        }
        res
    }
pub fn write ( data : & mut [ u8 ] , record : & Record ) -> Result < usize , ProgramError > { let offset = if let Some ( idx ) = record . index { // if the index was specified we will update existing record RefDB :: get_offset ( data , idx as usize ) ? } else { // otherwise either find a record with the supplied name or first available slot RefDB :: find_write_offset ( data , & record . name ) ? } ; let ( cur_counter , cur_name ) = RefDB :: get_record_counter_and_name ( data , offset ) ? ; if cur_counter > 0 { // if the counter was specified we check that value is equal to stored, // to make sure there were no intermediate updates if record . counter > 0 && cur_counter != record . counter as usize { return Err ( ProgramError :: Custom ( 409 ) ) ; } // check that we are either writing to an empty slot or record name matches if record . index . is_some ( ) && record . name != cur_name { return Err ( ProgramError :: Custom ( 409 ) ) ; } } // check that reference data type matches storage if RefDB :: get_reference_type ( data ) ? != record . reference . get_type ( ) { return Err ( ProgramError :: Custom ( 409 ) ) ; } // update storage counters let storage_counter = RefDB :: get_storage_counter ( data ) ? ; if ( storage_counter as u32 ) < u32 :: MAX { RefDB :: set_storage_counter ( data , storage_counter + 1 ) ? ; } else { RefDB :: set_storage_counter ( data , 1 ) ? ; } if cur_counter == 0 { let active_records = RefDB :: get_active_records ( data ) ? ; if ( active_records as u32 ) < u32 :: MAX { RefDB :: set_active_records ( data , active_records + 1 ) ? ; } } // write record let res = record . pack ( & mut data [ offset .. ] ) ; // update record counter if ( cur_counter as u16 ) < u16 :: MAX { RefDB :: set_record_counter ( data , offset , cur_counter + 1 ) ; } else { RefDB :: set_record_counter ( data , offset , 1 ) ; } res }
Writes the record to the storage.
Writes the record to the storage .
data
&mut [u8]
record
&Record
data
&mut [u8]
record
&Record