From 76684c89fcfcb0cf82a9a3d77c8eeee88cecf1d7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Mar 2021 04:48:22 +0800 Subject: [PATCH] refactor the traits to have a default implementation probably should be the *only* implementation because these are only concerned with IPC calls that really require changes in two separate crates, but I feel unconfident enough about the API that for now I want the flexibility to experiment with non-default APIs until I'm ready to freeze things. --- services/ime-plugin-api/src/lib.rs | 26 +++++++++++--------------- services/ime-plugin-shell/src/lib.rs | 6 ++++++ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/services/ime-plugin-api/src/lib.rs b/services/ime-plugin-api/src/lib.rs index 560890394..3c6c04619 100644 --- a/services/ime-plugin-api/src/lib.rs +++ b/services/ime-plugin-api/src/lib.rs @@ -7,14 +7,6 @@ pub struct Prediction { pub string: xous::String<4096>, } -pub trait PredictionApi { - fn get_prediction_triggers(self, cid: CID) -> Result; - fn unpick(self, cid: CID) -> Result<(), xous::Error>; - fn set_input(self, cid: CID, s: String<4096>) -> Result<(), xous::Error>; - fn feedback_picked(self, cid: CID, s: String<4096>) -> Result<(), xous::Error>; - fn get_prediction(self, cid: CID, index: u32) -> Result, xous::Error>; -} - #[derive(Debug, Default, Copy, Clone)] pub struct PredictionTriggers { /// trigger predictions on newline @@ -98,12 +90,16 @@ impl Into for Opcode { } } -// most implementation should not modify this, but it's made a trait in case for some weird reason it needs changes... +// provide a convenience version of the API for generic/standard calls #[derive(Debug, Default, Copy, Clone)] pub struct StandardPredictionPlugin {} +impl PredictionApi for StandardPredictionPlugin {} -impl PredictionApi for StandardPredictionPlugin { - fn get_prediction_triggers(self, cid: CID) -> Result { +// it's pretty dangerous to not use the default version of the API, because this is an IPC barrier +// and Rust can't check arguments and correctness between separate programs. +// In other words, you should really think twice before overriding these. +pub trait PredictionApi { + fn get_prediction_triggers(&self, cid: CID) -> Result { let response = xous::send_message(cid, Opcode::GetPredictionTriggers.into())?; if let xous::Result::Scalar1(code) = response { Ok(code.into()) @@ -112,12 +108,12 @@ impl PredictionApi for StandardPredictionPlugin { } } - fn unpick(self, cid: CID) -> Result<(), xous::Error> { + fn unpick(&self, cid: CID) -> Result<(), xous::Error> { xous::send_message(cid, Opcode::Unpick.into())?; Ok(()) } - fn set_input(self, cid: CID, s: String<4096>) -> Result<(), xous::Error> { + fn set_input(&self, cid: CID, s: String<4096>) -> Result<(), xous::Error> { use rkyv::Write; let rkyv_input = Opcode::Input(s); @@ -130,7 +126,7 @@ impl PredictionApi for StandardPredictionPlugin { Ok(()) } - fn feedback_picked(self, cid: CID, s: String<4096>) -> Result<(), xous::Error> { + fn feedback_picked(&self, cid: CID, s: String<4096>) -> Result<(), xous::Error> { use rkyv::Write; let rkyv_picked = Opcode::Picked(s); @@ -143,7 +139,7 @@ impl PredictionApi for StandardPredictionPlugin { Ok(()) } - fn get_prediction(self, cid: CID, index: u32) -> Result, xous::Error> { + fn get_prediction(&self, cid: CID, index: u32) -> Result, xous::Error> { use rkyv::Write; use rkyv::Unarchive; diff --git a/services/ime-plugin-shell/src/lib.rs b/services/ime-plugin-shell/src/lib.rs index 5b2eb54ee..2bbd1a844 100644 --- a/services/ime-plugin-shell/src/lib.rs +++ b/services/ime-plugin-shell/src/lib.rs @@ -2,3 +2,9 @@ pub use ime_plugin_api::*; +pub struct PredictionApiImpl { +} + +impl PredictionApi for PredictionApiImpl { + // inherit all the default methods +} \ No newline at end of file