Skip to main content

PkMethodAccessor

Trait PkMethodAccessor 

Source
pub trait PkMethodAccessor {
    // Required method
    fn call(
        &self,
        key: String,
        param: Vec<u8>,
    ) -> Result<Pin<Box<dyn Pollable>>, String>;
}
Expand description

Trait defining how to invoke methods by their string key.

This allows the PkCommand state machine to call arbitrary logic on the device. In std environments, a convenient implementation using std::collections::HashMap is provided as PkHashmapMethod. In no_std environments, you must provide your own implementation.

§Example

use pk_command::{PkMethodAccessor, Pollable};
use std::pin::Pin;
use std::task::Poll;

struct MyMethod;
impl Pollable for MyMethod {
    fn poll(&self) -> Poll<Result<Option<Vec<u8>>, String>> {
        Poll::Ready(Ok(Some(b"Hello from PK!".to_vec())))
    }
}

struct MyMethodStore;
impl PkMethodAccessor for MyMethodStore {
    fn call(&self, key: String, param: Vec<u8>) -> Result<Pin<Box<dyn Pollable>>, String> {
        if key == "GREET" {
            Ok(Box::pin(MyMethod))
        } else {
            Err("Method not found".to_string())
        }
    }
}

Required Methods§

Source

fn call( &self, key: String, param: Vec<u8>, ) -> Result<Pin<Box<dyn Pollable>>, String>

Calls a method with the given parameters.

§Arguments
  • key: The name of the method to call.
  • param: The parameters for the method, as a byte vector.
§Returns

A Result containing a pinned, boxed Pollable that will resolve to the method’s output, or an Err(String) if the method call cannot be initiated.

Implementors§

Source§

impl PkMethodAccessor for PkHashmapMethod

Available on crate feature std only.