-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add derive macros for Dispatch, ExtensionDispatch
This patch adds a new derive crate that can be used to derive implementations of the trussed::backend::Dispatch and trussed::serde_extensions::ExtensionDispatch traits.
- Loading branch information
1 parent
8b52bca
commit 79764af
Showing
14 changed files
with
704 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "trussed-derive" | ||
version = "0.1.0" | ||
|
||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license.workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dev-dependencies] | ||
serde = { version = "1.0", default-features = false } | ||
trussed = { path = "..", features = ["serde-extensions", "virt"] } | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0.51" | ||
quote = "1.0.23" | ||
syn = "2.0.53" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
mod backends { | ||
use trussed::backend::Backend; | ||
|
||
#[derive(Default)] | ||
pub struct ABackend; | ||
|
||
impl Backend for ABackend { | ||
type Context = (); | ||
} | ||
} | ||
|
||
enum Backend { | ||
A, | ||
} | ||
|
||
#[derive(Default, trussed_derive::Dispatch)] | ||
#[dispatch(backend_id = "Backend")] | ||
struct Dispatch { | ||
a: backends::ABackend, | ||
} | ||
|
||
fn main() { | ||
use trussed::{ | ||
backend::BackendId, | ||
client::CryptoClient, | ||
try_syscall, | ||
virt::{self, Ram}, | ||
Error, | ||
}; | ||
|
||
fn run(backends: &'static [BackendId<Backend>], expected: Option<Error>) { | ||
virt::with_platform(Ram::default(), |platform| { | ||
platform.run_client_with_backends( | ||
"test", | ||
Dispatch::default(), | ||
backends, | ||
|mut client| { | ||
assert_eq!(try_syscall!(client.random_bytes(42)).err(), expected); | ||
}, | ||
) | ||
}); | ||
} | ||
|
||
run(&[BackendId::Core], None); | ||
run( | ||
&[BackendId::Custom(Backend::A)], | ||
Some(Error::RequestNotAvailable), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use trussed::Error; | ||
|
||
mod backends { | ||
use super::extensions::{TestExtension, TestReply, TestRequest}; | ||
use trussed::{ | ||
backend::Backend, platform::Platform, serde_extensions::ExtensionImpl, | ||
service::ServiceResources, types::CoreContext, Error, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct ABackend; | ||
|
||
impl Backend for ABackend { | ||
type Context = (); | ||
} | ||
|
||
impl ExtensionImpl<TestExtension> for ABackend { | ||
fn extension_request<P: Platform>( | ||
&mut self, | ||
_core_ctx: &mut CoreContext, | ||
_backend_ctx: &mut Self::Context, | ||
_request: &TestRequest, | ||
_resources: &mut ServiceResources<P>, | ||
) -> Result<TestReply, Error> { | ||
Ok(TestReply) | ||
} | ||
} | ||
} | ||
|
||
mod extensions { | ||
use serde::{Deserialize, Serialize}; | ||
use trussed::{ | ||
serde_extensions::{Extension, ExtensionClient, ExtensionResult}, | ||
Error, | ||
}; | ||
|
||
pub struct TestExtension; | ||
|
||
impl Extension for TestExtension { | ||
type Request = TestRequest; | ||
type Reply = TestReply; | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub struct TestRequest; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub struct TestReply; | ||
|
||
impl TryFrom<TestReply> for () { | ||
type Error = Error; | ||
|
||
fn try_from(_reply: TestReply) -> Result<Self, Self::Error> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub trait TestClient { | ||
fn test(&mut self) -> ExtensionResult<'_, TestExtension, (), Self>; | ||
} | ||
|
||
impl<C: ExtensionClient<TestExtension>> TestClient for C { | ||
fn test(&mut self) -> ExtensionResult<'_, TestExtension, (), Self> { | ||
self.extension(TestRequest) | ||
} | ||
} | ||
|
||
pub struct SampleExtension; | ||
|
||
impl Extension for SampleExtension { | ||
type Request = SampleRequest; | ||
type Reply = SampleReply; | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub struct SampleRequest; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub struct SampleReply; | ||
} | ||
|
||
enum Backend { | ||
A, | ||
} | ||
|
||
enum Extension { | ||
Test = 0, | ||
Sample = 1, | ||
} | ||
|
||
impl From<Extension> for u8 { | ||
fn from(extension: Extension) -> u8 { | ||
extension as u8 | ||
} | ||
} | ||
|
||
impl TryFrom<u8> for Extension { | ||
type Error = Error; | ||
|
||
fn try_from(value: u8) -> Result<Self, Self::Error> { | ||
match value { | ||
0 => Ok(Self::Test), | ||
1 => Ok(Self::Sample), | ||
_ => Err(Error::InternalError), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, trussed_derive::ExtensionDispatch)] | ||
#[dispatch(backend_id = "Backend", extension_id = "Extension")] | ||
#[extensions( | ||
Test = "extensions::TestExtension", | ||
Sample = "extensions::SampleExtension" | ||
)] | ||
struct Dispatch { | ||
#[extensions("Test")] | ||
a: backends::ABackend, | ||
} | ||
|
||
fn main() { | ||
use extensions::TestClient; | ||
use trussed::{ | ||
backend::BackendId, | ||
try_syscall, | ||
virt::{self, Ram}, | ||
}; | ||
|
||
fn run(backends: &'static [BackendId<Backend>], expected: Option<Error>) { | ||
virt::with_platform(Ram::default(), |platform| { | ||
platform.run_client_with_backends( | ||
"test", | ||
Dispatch::default(), | ||
backends, | ||
|mut client| { | ||
assert_eq!(try_syscall!(client.test()).err(), expected); | ||
}, | ||
) | ||
}); | ||
} | ||
|
||
run(&[BackendId::Core], Some(Error::RequestNotAvailable)); | ||
run(&[BackendId::Custom(Backend::A)], None); | ||
} |
Oops, something went wrong.