From 02b5b7c4e53b1ed1eedd71c875f26b55a0d129d9 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 8 Feb 2023 10:20:36 +0100 Subject: [PATCH] Drop P parameter from backend and extension traits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Backend, Dispatch, ExtensionImpl and ExtensionDispatch traits don’t need to have the platform as a type parameter P. It is sufficient if the request handling methods receive it as a type parameter. This makes it easier to implement the traits. --- src/backend.rs | 10 +++++----- src/client.rs | 23 +++++++++++++---------- src/serde_extensions.rs | 18 +++++++++--------- src/service.rs | 14 +++++++------- src/virt.rs | 2 +- tests/backends.rs | 10 +++++----- tests/serde_extensions.rs | 26 +++++++++++++------------- 7 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index b8908bd96ce..d085146b483 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -27,13 +27,13 @@ pub enum BackendId { } /// A custom backend that can override the core request implementations. -pub trait Backend { +pub trait Backend { /// The context for requests handled by this backend. type Context: Default; /// Executes a request using this backend or returns [`Error::RequestNotAvailable`][] if it is /// not supported by this backend. - fn request( + fn request( &mut self, core_ctx: &mut CoreContext, backend_ctx: &mut Self::Context, @@ -51,7 +51,7 @@ pub trait Backend { /// Otherwise it can provide an implementation of this trait that defines which backends are /// supported. The backends that are used to execute a request can be selected when constructing a /// client using [`ClientBuilder::backends`][`crate::client::ClientBuilder::backends`]. -pub trait Dispatch { +pub trait Dispatch { /// The ID type for the custom backends used by this dispatch implementation. type BackendId: 'static; /// The context type used by this dispatch. @@ -59,7 +59,7 @@ pub trait Dispatch { /// Executes a request using a backend or returns [`Error::RequestNotAvailable`][] if it is not /// supported by the backend. - fn request( + fn request( &mut self, backend: &Self::BackendId, ctx: &mut Context, @@ -76,7 +76,7 @@ pub trait Dispatch { pub struct CoreOnly; #[cfg(not(feature = "serde-extensions"))] -impl Dispatch

for CoreOnly { +impl Dispatch for CoreOnly { type BackendId = NoId; type Context = crate::types::NoData; } diff --git a/src/client.rs b/src/client.rs index 88c83c32552..221a9e539d8 100644 --- a/src/client.rs +++ b/src/client.rs @@ -700,12 +700,12 @@ pub trait UiClient: PollClient { /// /// The maximum number of clients that can be created is defined by the `clients-?` features. If /// this number is exceeded, [`Error::ClientCountExceeded`][] is returned. -pub struct ClientBuilder = CoreOnly> { +pub struct ClientBuilder { id: PathBuf, backends: &'static [BackendId], } -impl ClientBuilder

{ +impl ClientBuilder { /// Creates a new client builder using the given client ID. /// /// Per default, the client does not support backends and always uses the Trussed core @@ -718,21 +718,21 @@ impl ClientBuilder

{ } } -impl> ClientBuilder { +impl ClientBuilder { /// Selects the backends to use for this client. /// /// If `backends` is empty, the Trussed core implementation is always used. - pub fn backends>( + pub fn backends( self, backends: &'static [BackendId], - ) -> ClientBuilder { + ) -> ClientBuilder { ClientBuilder { id: self.id, backends, } } - fn create_endpoint( + fn create_endpoint( self, service: &mut Service, ) -> Result, Error> { @@ -746,7 +746,10 @@ impl> ClientBuilder { /// /// This allocates a [`TrussedInterchange`][] and a /// [`ServiceEndpoint`][`crate::service::ServiceEndpoint`]. - pub fn prepare(self, service: &mut Service) -> Result, Error> { + pub fn prepare( + self, + service: &mut Service, + ) -> Result, Error> { self.create_endpoint(service) .map(|requester| PreparedClient::new(requester)) } @@ -757,12 +760,12 @@ impl> ClientBuilder { /// This struct already has an allocated [`TrussedInterchange`][] and /// [`ServiceEndpoint`][`crate::service::ServiceEndpoint`] but still needs a [`Syscall`][] /// implementation. -pub struct PreparedClient { +pub struct PreparedClient { requester: Requester, - _marker: PhantomData<(P, D)>, + _marker: PhantomData, } -impl PreparedClient { +impl PreparedClient { fn new(requester: Requester) -> Self { Self { requester, diff --git a/src/serde_extensions.rs b/src/serde_extensions.rs index 3fe540aaf4c..8c372d1a3e9 100644 --- a/src/serde_extensions.rs +++ b/src/serde_extensions.rs @@ -37,7 +37,7 @@ pub trait Extension { } /// Dispatches extension requests to custom backends. -pub trait ExtensionDispatch { +pub trait ExtensionDispatch { /// The ID type for the custom backends used by this dispatch implementation. type BackendId: 'static; /// The context type used by this dispatch. @@ -47,7 +47,7 @@ pub trait ExtensionDispatch { /// Executes a request using a backend or returns [`Error::RequestNotAvailable`][] if it is not /// supported by the backend. - fn core_request( + fn core_request( &mut self, backend: &Self::BackendId, ctx: &mut Context, @@ -59,7 +59,7 @@ pub trait ExtensionDispatch { } /// Executes an extension request using a backend or returns [`Error::RequestNotAvailable`][] /// if it is not supported by the backend. - fn extension_request( + fn extension_request( &mut self, backend: &Self::BackendId, extension: &Self::ExtensionId, @@ -72,11 +72,11 @@ pub trait ExtensionDispatch { } } -impl> Dispatch

for T { +impl Dispatch for T { type BackendId = T::BackendId; type Context = T::Context; - fn request( + fn request( &mut self, backend: &Self::BackendId, ctx: &mut Context, @@ -95,16 +95,16 @@ impl> Dispatch

for T { } } -impl ExtensionDispatch

for CoreOnly { +impl ExtensionDispatch for CoreOnly { type BackendId = NoId; type Context = types::NoData; type ExtensionId = NoId; } /// Implements an extension for a backend. -pub trait ExtensionImpl: Backend

{ +pub trait ExtensionImpl: Backend { /// Handles an extension request. - fn extension_request( + fn extension_request( &mut self, core_ctx: &mut CoreContext, backend_ctx: &mut Self::Context, @@ -115,7 +115,7 @@ pub trait ExtensionImpl: Backend

{ /// Handles an extension request and performs the necessary serialization and deserialization /// between [`request::SerdeExtension`][] and [`Extension::Request`][] as well as /// [`reply::SerdeExtension`][] and [`Extension::Reply`][]. - fn extension_request_serialized( + fn extension_request_serialized( &mut self, core_ctx: &mut CoreContext, backend_ctx: &mut Self::Context, diff --git a/src/service.rs b/src/service.rs index bd97b6e38d3..fa683045e22 100644 --- a/src/service.rs +++ b/src/service.rs @@ -82,7 +82,7 @@ impl ServiceResources

{ pub struct Service where P: Platform, - D: Dispatch

, + D: Dispatch, { eps: Vec, { MAX_SERVICE_CLIENTS::USIZE }>, resources: ServiceResources

, @@ -90,7 +90,7 @@ where } // need to be able to send crypto service to an interrupt handler -unsafe impl> Send for Service {} +unsafe impl Send for Service {} impl ServiceResources

{ pub fn certstore(&mut self, ctx: &CoreContext) -> Result> { @@ -119,7 +119,7 @@ impl ServiceResources

{ .map_err(|_| Error::EntropyMalfunction) } - pub fn dispatch>( + pub fn dispatch( &mut self, dispatch: &mut D, backend: &BackendId, @@ -690,7 +690,7 @@ impl Service

{ } } -impl> Service { +impl Service { pub fn with_dispatch(platform: P, dispatch: D) -> Self { let resources = ServiceResources::new(platform); Self { @@ -738,7 +738,7 @@ impl Service

{ } } -impl> Service { +impl Service { pub fn add_endpoint( &mut self, interchange: Responder, @@ -842,7 +842,7 @@ impl> Service { impl crate::client::Syscall for &mut Service where P: Platform, - D: Dispatch

, + D: Dispatch, { fn syscall(&mut self) { self.process(); @@ -852,7 +852,7 @@ where impl crate::client::Syscall for Service where P: Platform, - D: Dispatch

, + D: Dispatch, { fn syscall(&mut self) { self.process(); diff --git a/src/virt.rs b/src/virt.rs index 333cde618b4..10fdce877eb 100644 --- a/src/virt.rs +++ b/src/virt.rs @@ -90,7 +90,7 @@ impl Platform { test(client) } - pub fn run_client_with_backends>( + pub fn run_client_with_backends( self, client_id: &str, dispatch: D, diff --git a/tests/backends.rs b/tests/backends.rs index 0b2548ae005..1b7c423a9cc 100644 --- a/tests/backends.rs +++ b/tests/backends.rs @@ -26,16 +26,16 @@ struct Dispatch { test: TestBackend, } -impl backend::Dispatch for Dispatch { +impl backend::Dispatch for Dispatch { type BackendId = Backend; type Context = (); - fn request( + fn request( &mut self, backend: &Self::BackendId, ctx: &mut Context<()>, request: &Request, - resources: &mut ServiceResources, + resources: &mut ServiceResources

, ) -> Result { match backend { Backend::Test => { @@ -49,10 +49,10 @@ impl backend::Dispatch for Dispatch { #[derive(Default)] struct TestBackend; -impl backend::Backend

for TestBackend { +impl backend::Backend for TestBackend { type Context = (); - fn request( + fn request( &mut self, _core_ctx: &mut CoreContext, _backend_ctx: &mut Self::Context, diff --git a/tests/serde_extensions.rs b/tests/serde_extensions.rs index cd6dccb7912..611a13f5533 100644 --- a/tests/serde_extensions.rs +++ b/tests/serde_extensions.rs @@ -236,12 +236,12 @@ mod backends { /// Implements TestExtension pub struct TestBackend; - impl Backend

for TestBackend { + impl Backend for TestBackend { type Context = TestContext; } - impl ExtensionImpl for TestBackend { - fn extension_request( + impl ExtensionImpl for TestBackend { + fn extension_request( &mut self, _core_ctx: &mut CoreContext, backend_ctx: &mut TestContext, @@ -273,12 +273,12 @@ mod backends { /// Implements SampleExtension and TestExtension pub struct SampleBackend; - impl Backend

for SampleBackend { + impl Backend for SampleBackend { type Context = SampleContext; } - impl ExtensionImpl for SampleBackend { - fn extension_request( + impl ExtensionImpl for SampleBackend { + fn extension_request( &mut self, _core_ctx: &mut CoreContext, backend_ctx: &mut SampleContext, @@ -301,8 +301,8 @@ mod backends { } } - impl ExtensionImpl for SampleBackend { - fn extension_request( + impl ExtensionImpl for SampleBackend { + fn extension_request( &mut self, _core_ctx: &mut CoreContext, backend_ctx: &mut SampleContext, @@ -386,12 +386,12 @@ mod runner { sample: SampleContext, } - impl ExtensionDispatch

for Backends { + impl ExtensionDispatch for Backends { type BackendId = id::Backend; type Context = BackendsContext; type ExtensionId = id::Extension; - fn core_request( + fn core_request( &mut self, backend: &Self::BackendId, ctx: &mut Context, @@ -410,7 +410,7 @@ mod runner { } } - fn extension_request( + fn extension_request( &mut self, backend: &Self::BackendId, extension: &Self::ExtensionId, @@ -429,14 +429,14 @@ mod runner { id::Extension::Sample => Err(Error::RequestNotAvailable), }, id::Backend::Sample => match extension { - id::Extension::Test => >::extension_request_serialized( + id::Extension::Test => >::extension_request_serialized( &mut self.sample, &mut ctx.core, &mut ctx.backends.sample, request, resources, ), - id::Extension::Sample => >::extension_request_serialized( + id::Extension::Sample => >::extension_request_serialized( &mut self.sample, &mut ctx.core, &mut ctx.backends.sample,