Skip to content

Commit

Permalink
allow for Server/WireTx without Clone (#60)
Browse files Browse the repository at this point in the history
Server was required to implement `Clone`. This is only needed by its
`sender()` function which clones the transmit wire. This removes the hard
requirement, implementing `sender()` only when the used `WireTx` is also
`Clone`.
  • Loading branch information
hvraven authored Nov 27, 2024
1 parent aa96e69 commit d504e61
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion example/firmware/src/bin/comms-01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async fn main(spawner: Spawner) {
let dispatcher = MyApp::new(context, spawner.into());
let vkk = dispatcher.min_key_len();
let mut server: AppServer = Server::new(
&tx_impl,
tx_impl,
rx_impl,
pbufs.rx_buf.as_mut_slice(),
dispatcher,
Expand Down
2 changes: 1 addition & 1 deletion example/firmware/src/bin/comms-02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async fn main(spawner: Spawner) {
let dispatcher = MyApp::new(context, spawner.into());
let vkk = dispatcher.min_key_len();
let mut server: AppServer = Server::new(
&tx_impl,
tx_impl,
rx_impl,
pbufs.rx_buf.as_mut_slice(),
dispatcher,
Expand Down
2 changes: 1 addition & 1 deletion example/firmware/src/bin/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async fn main(spawner: Spawner) {
let dispatcher = MyApp::new(context, spawner.into());
let vkk = dispatcher.min_key_len();
let server: AppServer = Server::new(
&tx_impl,
tx_impl,
rx_impl,
pbufs.rx_buf.as_mut_slice(),
dispatcher,
Expand Down
2 changes: 1 addition & 1 deletion example/firmware/src/bin/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async fn main(spawner: Spawner) {
let dispatcher = MyApp::new(context, spawner.into());
let vkk = dispatcher.min_key_len();
let server: AppServer = Server::new(
&tx_impl,
tx_impl,
rx_impl,
pbufs.rx_buf.as_mut_slice(),
dispatcher,
Expand Down
4 changes: 2 additions & 2 deletions source/postcard-rpc/src/server/impls/test_channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub mod dispatch_impl {
{
let buf = vec![0; settings.buf];
Server::new(
&settings.tx,
settings.tx,
settings.rx,
buf.into_boxed_slice(),
dispatch,
Expand All @@ -91,7 +91,7 @@ pub mod dispatch_impl {
settings.rx.set_stopper(stopper.clone());
let buf = vec![0; settings.buf];
let me = Server::new(
&settings.tx,
settings.tx,
settings.rx,
buf.into_boxed_slice(),
dispatch,
Expand Down
27 changes: 16 additions & 11 deletions source/postcard-rpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
//////////////////////////////////////////////////////////////////////////////

/// This trait defines how the server sends frames to the client
pub trait WireTx: Clone {
pub trait WireTx {
/// The error type of this connection.
///
/// For simple cases, you can use [`WireTxErrorKind`] directly. You can also
Expand Down Expand Up @@ -401,23 +401,15 @@ where
/// * a buffer used for receiving frames
/// * The user provided dispatching method, usually generated by [`define_dispatch!()`][crate::define_dispatch]
/// * a [`VarKeyKind`], which controls the key sizes sent by the [`WireTx`] impl
pub fn new(tx: &Tx, rx: Rx, buf: Buf, dis: D, kkind: VarKeyKind) -> Self {
pub fn new(tx: Tx, rx: Rx, buf: Buf, dis: D, kkind: VarKeyKind) -> Self {
Self {
tx: Sender {
tx: tx.clone(),
kkind,
},
tx: Sender { tx, kkind },
rx,
buf,
dis,
}
}

/// Get a copy of the [`Sender`] to pass to tasks that need it
pub fn sender(&self) -> Sender<Tx> {
self.tx.clone()
}

/// Run until a fatal error occurs
///
/// The server will receive frames, and dispatch them. When a fatal error occurs,
Expand Down Expand Up @@ -462,6 +454,19 @@ where
}
}

impl<Tx, Rx, Buf, D> Server<Tx, Rx, Buf, D>
where
Tx: WireTx + Clone,
Rx: WireRx,
Buf: DerefMut<Target = [u8]>,
D: Dispatch<Tx = Tx>,
{
/// Get a copy of the [`Sender`] to pass to tasks that need it
pub fn sender(&self) -> Sender<Tx> {
self.tx.clone()
}
}

//////////////////////////////////////////////////////////////////////////////
// DISPATCH TRAIT
//////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit d504e61

Please sign in to comment.