Skip to content

Commit 5d53357

Browse files
committed
Use core::error::Error trait
Signed-off-by: Denis Varlakov <denis@dfns.co>
1 parent 57375b0 commit 5d53357

File tree

13 files changed

+94
-149
lines changed

13 files changed

+94
-149
lines changed

Cargo.lock

+14-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/random-generation-protocol/Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ rand_core = { version = "0.6", default-features = false }
1313
sha2 = { version = "0.10", default-features = false }
1414
serde = { version = "1", default-features = false, features = ["derive"] }
1515

16-
displaydoc = { version = "0.2", default-features = false }
17-
thiserror = { version = "1", optional = true }
16+
thiserror = { version = "2", default-features = false }
1817

1918
# We don't use it directy, but we need to enable `serde` feature
2019
generic-array = { version = "0.14", features = ["serde"] }
@@ -26,5 +25,3 @@ hex = "0.4"
2625
rand_dev = "0.1"
2726
rand = "0.8"
2827

29-
[features]
30-
std = ["thiserror"]

examples/random-generation-protocol/src/lib.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#![no_std]
44
#![forbid(unused_crate_dependencies, missing_docs)]
55

6-
#[cfg(any(feature = "std", test))]
6+
#[cfg(test)]
77
extern crate std;
88

99
extern crate alloc;
1010

1111
mod _unused_deps {
12-
// We don't use it directy, but we need to enable `serde` feature
12+
// We don't use it directly, but we need to enable `serde` feature
1313
use generic_array as _;
1414
}
1515

@@ -132,28 +132,23 @@ where
132132
}
133133

134134
/// Protocol error
135-
#[derive(Debug, displaydoc::Display)]
136-
#[cfg_attr(feature = "std", derive(thiserror::Error))]
135+
#[derive(Debug, thiserror::Error)]
137136
pub enum Error<RecvErr, SendErr> {
138137
/// Couldn't send a message in the first round
139-
#[displaydoc("send a message at round 1")]
140-
Round1Send(#[cfg_attr(feature = "std", source)] SendErr),
138+
#[error("send a message at round 1")]
139+
Round1Send(#[source] SendErr),
141140
/// Couldn't receive a message in the first round
142-
#[displaydoc("receive messages at round 1")]
143-
Round1Receive(
144-
#[cfg_attr(feature = "std", source)] CompleteRoundError<RoundInputError, RecvErr>,
145-
),
141+
#[error("receive messages at round 1")]
142+
Round1Receive(#[source] CompleteRoundError<RoundInputError, RecvErr>),
146143
/// Couldn't send a message in the second round
147-
#[displaydoc("send a message at round 2")]
148-
Round2Send(#[cfg_attr(feature = "std", source)] SendErr),
144+
#[error("send a message at round 2")]
145+
Round2Send(#[source] SendErr),
149146
/// Couldn't receive a message in the second round
150-
#[displaydoc("receive messages at round 2")]
151-
Round2Receive(
152-
#[cfg_attr(feature = "std", source)] CompleteRoundError<RoundInputError, RecvErr>,
153-
),
147+
#[error("receive messages at round 2")]
148+
Round2Receive(#[source] CompleteRoundError<RoundInputError, RecvErr>),
154149

155150
/// Some of the parties cheated
156-
#[displaydoc("malicious parties: {guilty_parties:?}")]
151+
#[error("malicious parties: {guilty_parties:?}")]
157152
PartiesOpenedRandomnessDoesntMatchCommitment {
158153
/// List of cheated parties
159154
guilty_parties: Vec<Blame>,

round-based/CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## v0.4.0
2-
* Improve ergonomics of protocol simulation, which is used for writing tests [#14]
2+
* BREAKING: Improve ergonomics of protocol simulation, which is used for writing tests [#14]
33
* Remove `dev` feature, it's replaced with `sim` and `sim-async`
44
* `round_based::simulation` module is renamed into `round_based::sim`
55
* `round_based::simulation::Simulation` is renamed and moved to `round_based::sim::async_env::Network`
@@ -12,6 +12,9 @@
1212
* When `sim-async` feature is enabled, you can use `round_based::sim::async_env::{run, run_with_setup, ...}`,
1313
but typically you don't want to use them
1414
* `round_based::simulation::SimulationSync` has been renamed to `round_based::sim::Simulation`
15+
* Use `core::error::Error` trait which is now always implemented for all errors regardless whether `std` feature
16+
is enabled or not [#14]
17+
* Update `thiserror` dependency to v2
1518

1619
Migration guidelines:
1720
* Replace `dev` feature with `sim`

round-based/Cargo.toml

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1919
futures-util = { version = "0.3", default-features = false, features = ["sink"] }
2020
phantom-type = { version = "0.3", default-features = false }
2121
tracing = { version = "0.1", default-features = false }
22-
thiserror = { version = "1", optional = true }
23-
displaydoc = { version = "0.2", default-features = false }
22+
thiserror = { version = "2", default-features = false }
2423

2524
round-based-derive = { version = "0.2", optional = true, path = "../round-based-derive" }
2625

@@ -40,13 +39,13 @@ rand_dev = "0.1"
4039
anyhow = "1"
4140

4241
[features]
43-
default = ["std"]
42+
default = []
4443
state-machine = []
45-
sim = ["std", "state-machine"]
44+
sim = ["state-machine"]
4645
sim-async = ["sim", "tokio/sync", "tokio-stream", "futures-util/alloc"]
4746
derive = ["round-based-derive"]
4847
runtime-tokio = ["tokio"]
49-
std = ["thiserror"]
48+
std = []
5049

5150
[[test]]
5251
name = "derive"

round-based/src/delivery.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use futures_util::{Sink, Stream};
22

3-
use crate::StdError;
4-
53
/// Networking abstraction
64
///
75
/// Basically, it's pair of channels: [`Stream`] for receiving messages, and [`Sink`] for sending
@@ -12,9 +10,9 @@ pub trait Delivery<M> {
1210
/// Incoming delivery channel
1311
type Receive: Stream<Item = Result<Incoming<M>, Self::ReceiveError>> + Unpin;
1412
/// Error of outgoing delivery channel
15-
type SendError: StdError + Send + Sync + 'static;
13+
type SendError: core::error::Error + Send + Sync + 'static;
1614
/// Error of incoming delivery channel
17-
type ReceiveError: StdError + Send + Sync + 'static;
15+
type ReceiveError: core::error::Error + Send + Sync + 'static;
1816
/// Returns a pair of incoming and outgoing delivery channels
1917
fn split(self) -> (Self::Receive, Self::Send);
2018
}
@@ -23,8 +21,8 @@ impl<M, I, O, IErr, OErr> Delivery<M> for (I, O)
2321
where
2422
I: Stream<Item = Result<Incoming<M>, IErr>> + Unpin,
2523
O: Sink<Outgoing<M>, Error = OErr> + Unpin,
26-
IErr: StdError + Send + Sync + 'static,
27-
OErr: StdError + Send + Sync + 'static,
24+
IErr: core::error::Error + Send + Sync + 'static,
25+
OErr: core::error::Error + Send + Sync + 'static,
2826
{
2927
type Send = O;
3028
type Receive = I;

round-based/src/lib.rs

-12
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,3 @@ pub mod _docs;
9393
/// See [`ProtocolMessage`] docs for more details
9494
#[cfg(feature = "derive")]
9595
pub use round_based_derive::ProtocolMessage;
96-
97-
mod std_error {
98-
#[cfg(feature = "std")]
99-
pub use std::error::Error as StdError;
100-
101-
#[cfg(not(feature = "std"))]
102-
pub trait StdError: core::fmt::Display + core::fmt::Debug {}
103-
#[cfg(not(feature = "std"))]
104-
impl<E: core::fmt::Display + core::fmt::Debug> StdError for E {}
105-
}
106-
107-
use std_error::StdError;

round-based/src/party.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use phantom_type::PhantomType;
3434

3535
use crate::delivery::Delivery;
3636
use crate::runtime::{self, AsyncRuntime};
37-
use crate::StdError;
3837

3938
/// Party of MPC protocol (trait)
4039
///
@@ -82,9 +81,9 @@ pub trait Mpc: internal::Sealed {
8281
type Runtime: AsyncRuntime;
8382

8483
/// Sending message error
85-
type SendError: StdError + Send + Sync + 'static;
84+
type SendError: core::error::Error + Send + Sync + 'static;
8685
/// Receiving message error
87-
type ReceiveError: StdError + Send + Sync + 'static;
86+
type ReceiveError: core::error::Error + Send + Sync + 'static;
8887

8988
/// Converts into [`MpcParty`]
9089
fn into_party(self) -> MpcParty<Self::ProtocolMessage, Self::Delivery, Self::Runtime>;
@@ -142,8 +141,8 @@ impl<M, D, B> internal::Sealed for MpcParty<M, D, B> {}
142141
impl<M, D, R> Mpc for MpcParty<M, D, R>
143142
where
144143
D: Delivery<M>,
145-
D::SendError: StdError + Send + Sync + 'static,
146-
D::ReceiveError: StdError + Send + Sync + 'static,
144+
D::SendError: core::error::Error + Send + Sync + 'static,
145+
D::ReceiveError: core::error::Error + Send + Sync + 'static,
147146
R: AsyncRuntime,
148147
{
149148
type ProtocolMessage = M;

0 commit comments

Comments
 (0)