Skip to main content

PkVariableAccessor

Trait PkVariableAccessor 

Source
pub trait PkVariableAccessor {
    // Required methods
    fn get(&self, key: String) -> Option<Vec<u8>>;
    fn set(&self, key: String, value: Vec<u8>) -> Result<(), String>;
}
Expand description

Trait defining how to access (get/set) variables by their string key.

This allows the PkCommand state machine to be generic over the actual variable storage. In std environments, a convenient implementation using std::collections::HashMap is provided as PkHashmapVariable. In no_std environments, you must provide your own implementation.

§Example

use pk_command::PkVariableAccessor;

struct MyVariableStore;
impl PkVariableAccessor for MyVariableStore {
    fn get(&self, key: String) -> Option<Vec<u8>> {
        if key == "VERSION" {
            Some(b"1.0.0".to_vec())
        } else {
            None
        }
    }
    fn set(&self, key: String, value: Vec<u8>) -> Result<(), String> {
        // Logic to store the value
        Ok(())
    }
}

Required Methods§

Source

fn get(&self, key: String) -> Option<Vec<u8>>

Retrieves the value of a variable.

§Arguments
  • key: The name of the variable to retrieve.
§Returns

Some(Vec<u8>) containing the variable’s data if found, or None otherwise.

Source

fn set(&self, key: String, value: Vec<u8>) -> Result<(), String>

Sets the value of a variable.

§Arguments
  • key: The name of the variable to set.
  • value: The new data for the variable.
§Returns

Ok(()) if successful, or an Err(String) describing the error.

Implementors§

Source§

impl PkVariableAccessor for PkHashmapVariable

Available on crate feature std only.