Skip to content

Commit

Permalink
Mark Router::add_route as deprecated (#367)
Browse files Browse the repository at this point in the history
* Propagate ipc errors to users of TypedRouterHandle

Signed-off-by: Simon Wülker <[email protected]>

* Deprecate Router::add_route

Signed-off-by: Simon Wülker <[email protected]>

* Bump Cargo.toml version

Signed-off-by: Simon Wülker <[email protected]>

* Use semver-compliant version.

Signed-off-by: Josh Matthews <[email protected]>

---------

Signed-off-by: Simon Wülker <[email protected]>
Signed-off-by: Josh Matthews <[email protected]>
Co-authored-by: Josh Matthews <[email protected]>
  • Loading branch information
simonwuelker and jdm authored Oct 15, 2024
1 parent 5264d06 commit 4e51f95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ipc-channel"
version = "0.18.3"
version = "0.19.0"
description = "A multiprocess drop-in replacement for Rust channels"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
Expand Down
12 changes: 9 additions & 3 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl RouterProxy {

/// Add a new (receiver, callback) pair to the router, and send a wakeup message
/// to the router.
///
/// Consider using [add_typed_route](Self::add_typed_route) instead, which prevents
/// mismatches between the receiver and callback types.
#[deprecated(since = "0.19.0", note = "please use 'add_typed_route' instead")]
pub fn add_route(&self, receiver: OpaqueIpcReceiver, callback: RouterHandler) {
let comm = self.comm.lock().unwrap();

Expand All @@ -83,9 +87,11 @@ impl RouterProxy {
{
// Before passing the message on to the callback, turn it into the appropriate type
let modified_callback = move |msg: IpcMessage| {
let typed_message = msg.to::<T>().unwrap();
let typed_message = msg.to::<T>();
callback(typed_message)
};

#[allow(deprecated)]
self.add_route(receiver.to_opaque(), Box::new(modified_callback));
}

Expand Down Expand Up @@ -123,7 +129,7 @@ impl RouterProxy {
{
self.add_typed_route(
ipc_receiver,
Box::new(move |message| drop(crossbeam_sender.send(message))),
Box::new(move |message| drop(crossbeam_sender.send(message.unwrap()))),
)
}

Expand Down Expand Up @@ -234,4 +240,4 @@ enum RouterMsg {
pub type RouterHandler = Box<dyn FnMut(IpcMessage) + Send>;

/// Like [RouterHandler] but includes the type that will be passed to the callback
pub type TypedRouterHandler<T> = Box<dyn FnMut(T) + Send>;
pub type TypedRouterHandler<T> = Box<dyn FnMut(Result<T, bincode::Error>) + Send>;
23 changes: 12 additions & 11 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ fn router_simple_global() {
tx.send(person.clone()).unwrap();

let (callback_fired_sender, callback_fired_receiver) = crossbeam_channel::unbounded::<Person>();
#[allow(deprecated)]
ROUTER.add_route(
rx.to_opaque(),
Box::new(move |person| {
Expand All @@ -293,7 +294,7 @@ fn router_simple_global() {
ROUTER.add_typed_route(
rx,
Box::new(move |message| {
callback_fired_sender.send(message).unwrap();
callback_fired_sender.send(message.unwrap()).unwrap();
}),
);
let received_message = callback_fired_receiver.recv().unwrap();
Expand All @@ -308,10 +309,10 @@ fn router_simple_global() {
tx.send(person.clone()).unwrap();

let (callback_fired_sender, callback_fired_receiver) = crossbeam_channel::unbounded::<Person>();
ROUTER.add_route(
rx.to_opaque(),
ROUTER.add_typed_route(
rx,
Box::new(move |person| {
callback_fired_sender.send(person.to().unwrap()).unwrap();
callback_fired_sender.send(person.unwrap()).unwrap();
}),
);

Expand Down Expand Up @@ -389,8 +390,8 @@ fn router_drops_callbacks_on_sender_shutdown() {
let dropper = Dropper { sender: drop_tx };

let router = RouterProxy::new();
router.add_route(
rx0.to_opaque(),
router.add_typed_route(
rx0,
Box::new(move |_| {
let _ = &dropper;
}),
Expand All @@ -416,8 +417,8 @@ fn router_drops_callbacks_on_cloned_sender_shutdown() {
let dropper = Dropper { sender: drop_tx };

let router = RouterProxy::new();
router.add_route(
rx0.to_opaque(),
router.add_typed_route(
rx0,
Box::new(move |_| {
let _ = &dropper;
}),
Expand All @@ -441,9 +442,9 @@ fn router_big_data() {
let (callback_fired_sender, callback_fired_receiver) =
crossbeam_channel::unbounded::<Vec<Person>>();
let router = RouterProxy::new();
router.add_route(
rx.to_opaque(),
Box::new(move |people| callback_fired_sender.send(people.to().unwrap()).unwrap()),
router.add_typed_route(
rx,
Box::new(move |people| callback_fired_sender.send(people.unwrap()).unwrap()),
);
let received_people = callback_fired_receiver.recv().unwrap();
assert_eq!(received_people, people);
Expand Down

0 comments on commit 4e51f95

Please sign in to comment.