Skip to content

Commit a34969b

Browse files
author
Rusty Rain
authored
Merge pull request #8 from algesten/anyhow
Refactor out use of anyhow
2 parents afeae7e + 85be6c0 commit a34969b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+165
-179
lines changed

crates/sctp/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ repository = "https://github.com/webrtc-rs/sctp"
1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313

1414
[dependencies]
15-
util = { package = "webrtc-util", version = "0.4.3", default-features = false, features = ["conn"] }
15+
util = { package = "webrtc-util", version = "0.5.0", default-features = false, features = ["conn"] }
1616
tokio = { version = "1.12.0", features = ["full"] }
1717
bytes = "1"
1818
rand = "0.8.0"
1919
crc = "2.0.0"
2020
async-trait = "0.1"
2121
log = "0.4"
22-
thiserror = "1.0.25"
23-
anyhow = "1.0.41"
22+
thiserror = "1.0"
2423

2524
[dev-dependencies]
2625
tokio-test = "0.4"

crates/sctp/examples/ping.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use webrtc_sctp::association::*;
22
use webrtc_sctp::chunk::chunk_payload_data::PayloadProtocolIdentifier;
33
use webrtc_sctp::stream::*;
4+
use webrtc_sctp::Error;
45

5-
use anyhow::Result;
66
use bytes::Bytes;
77
use clap::{App, AppSettings, Arg};
88
use std::sync::Arc;
@@ -13,7 +13,7 @@ use tokio::sync::mpsc;
1313
// RUST_LOG=trace cargo run --color=always --package webrtc-sctp --example ping -- --server 0.0.0.0:5678
1414

1515
#[tokio::main]
16-
async fn main() -> Result<()> {
16+
async fn main() -> Result<(), Error> {
1717
/*env_logger::Builder::new()
1818
.format(|buf, record| {
1919
writeln!(
@@ -88,7 +88,7 @@ async fn main() -> Result<()> {
8888
}
8989

9090
println!("finished send ping");
91-
Result::<()>::Ok(())
91+
Result::<(), Error>::Ok(())
9292
});
9393

9494
let (done_tx, mut done_rx) = mpsc::channel::<()>(1);

crates/sctp/examples/pong.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use webrtc_sctp::association::*;
22
use webrtc_sctp::stream::*;
3+
use webrtc_sctp::Error;
34

4-
use anyhow::Result;
55
use bytes::Bytes;
66
use clap::{App, AppSettings, Arg};
77
use std::sync::Arc;
@@ -14,7 +14,7 @@ use util::{conn::conn_disconnected_packet::DisconnectedPacketConn, Conn};
1414
// RUST_LOG=trace cargo run --color=always --package webrtc-sctp --example pong -- --host 0.0.0.0:5678
1515

1616
#[tokio::main]
17-
async fn main() -> Result<()> {
17+
async fn main() -> Result<(), Error> {
1818
/*env_logger::Builder::new()
1919
.format(|buf, record| {
2020
writeln!(
@@ -92,7 +92,7 @@ async fn main() -> Result<()> {
9292
println!("finished ping-pong");
9393
drop(done_tx);
9494

95-
Result::<()>::Ok(())
95+
Result::<(), Error>::Ok(())
9696
});
9797

9898
println!("Waiting for Ctrl-C...");

crates/sctp/src/association/association_internal.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ mod association_internal_test;
33

44
use super::*;
55

6-
use anyhow::Result;
76
use async_trait::async_trait;
87
use std::sync::atomic::AtomicBool;
98

@@ -186,7 +185,7 @@ impl AssociationInternal {
186185

187186
Ok(())
188187
} else {
189-
Err(Error::ErrInitNotStoredToSend.into())
188+
Err(Error::ErrInitNotStoredToSend)
190189
}
191190
}
192191

@@ -206,7 +205,7 @@ impl AssociationInternal {
206205
self.awake_write_loop();
207206
Ok(())
208207
} else {
209-
Err(Error::ErrCookieEchoNotStoredToSend.into())
208+
Err(Error::ErrCookieEchoNotStoredToSend)
210209
}
211210
}
212211

@@ -670,7 +669,7 @@ impl AssociationInternal {
670669
{
671670
// 5.2.2. Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
672671
// COOKIE-WAIT, and SHUTDOWN-ACK-SENT
673-
return Err(Error::ErrHandleInitState.into());
672+
return Err(Error::ErrHandleInitState);
674673
}
675674

676675
// Should we be setting any of these permanently until we've ACKed further?
@@ -819,7 +818,7 @@ impl AssociationInternal {
819818

820819
Ok(vec![])
821820
} else {
822-
Err(Error::ErrInitAckNoCookie.into())
821+
Err(Error::ErrInitAckNoCookie)
823822
}
824823
}
825824

@@ -1049,14 +1048,14 @@ impl AssociationInternal {
10491048
default_payload_type: PayloadProtocolIdentifier,
10501049
) -> Result<Arc<Stream>> {
10511050
if self.streams.contains_key(&stream_identifier) {
1052-
return Err(Error::ErrStreamAlreadyExist.into());
1051+
return Err(Error::ErrStreamAlreadyExist);
10531052
}
10541053

10551054
if let Some(s) = self.create_stream(stream_identifier, false) {
10561055
s.set_default_payload_type(default_payload_type);
10571056
Ok(Arc::clone(&s))
10581057
} else {
1059-
Err(Error::ErrStreamCreateFailed.into())
1058+
Err(Error::ErrStreamCreateFailed)
10601059
}
10611060
}
10621061

@@ -1153,7 +1152,7 @@ impl AssociationInternal {
11531152
self.min_tsn2measure_rtt = self.my_next_tsn;
11541153
let rtt = match SystemTime::now().duration_since(c.since) {
11551154
Ok(rtt) => rtt,
1156-
Err(_) => return Err(Error::ErrInvalidSystemTime.into()),
1155+
Err(_) => return Err(Error::ErrInvalidSystemTime),
11571156
};
11581157
let srtt = self.rto_mgr.set_new_rtt(rtt.as_millis() as u64);
11591158
log::trace!(
@@ -1171,7 +1170,7 @@ impl AssociationInternal {
11711170
self.in_fast_recovery = false;
11721171
}
11731172
} else {
1174-
return Err(Error::ErrInflightQueueTsnPop.into());
1173+
return Err(Error::ErrInflightQueueTsnPop);
11751174
}
11761175

11771176
i += 1;
@@ -1210,7 +1209,7 @@ impl AssociationInternal {
12101209
self.min_tsn2measure_rtt = self.my_next_tsn;
12111210
let rtt = match SystemTime::now().duration_since(c.since) {
12121211
Ok(rtt) => rtt,
1213-
Err(_) => return Err(Error::ErrInvalidSystemTime.into()),
1212+
Err(_) => return Err(Error::ErrInvalidSystemTime),
12141213
};
12151214
let srtt = self.rto_mgr.set_new_rtt(rtt.as_millis() as u64);
12161215
log::trace!(
@@ -1227,7 +1226,7 @@ impl AssociationInternal {
12271226
}
12281227
}
12291228
} else {
1230-
return Err(Error::ErrTsnRequestNotExist.into());
1229+
return Err(Error::ErrTsnRequestNotExist);
12311230
}
12321231
}
12331232
}
@@ -1367,7 +1366,7 @@ impl AssociationInternal {
13671366
}
13681367
}
13691368
} else {
1370-
return Err(Error::ErrTsnRequestNotExist.into());
1369+
return Err(Error::ErrTsnRequestNotExist);
13711370
}
13721371

13731372
tsn += 1;
@@ -1749,7 +1748,7 @@ impl AssociationInternal {
17491748
async fn send_reset_request(&mut self, stream_identifier: u16) -> Result<()> {
17501749
let state = self.get_state();
17511750
if state != AssociationState::Established {
1752-
return Err(Error::ErrResetPacketInStateNotExist.into());
1751+
return Err(Error::ErrResetPacketInStateNotExist);
17531752
}
17541753

17551754
// Create DATA chunk which only contains valid stream identifier with
@@ -1786,7 +1785,7 @@ impl AssociationInternal {
17861785
}
17871786
Ok(None)
17881787
} else {
1789-
Err(Error::ErrParamterType.into())
1788+
Err(Error::ErrParamterType)
17901789
}
17911790
}
17921791

@@ -1975,7 +1974,7 @@ impl AssociationInternal {
19751974
async fn send_payload_data(&mut self, chunks: Vec<ChunkPayloadData>) -> Result<()> {
19761975
let state = self.get_state();
19771976
if state != AssociationState::Established {
1978-
return Err(Error::ErrPayloadDataStateNotExist.into());
1977+
return Err(Error::ErrPayloadDataStateNotExist);
19791978
}
19801979

19811980
// Push the chunks into the pending queue first.
@@ -2154,7 +2153,7 @@ impl AssociationInternal {
21542153
} else if chunk_any.downcast_ref::<ChunkAbort>().is_some()
21552154
|| chunk_any.downcast_ref::<ChunkError>().is_some()
21562155
{
2157-
return Err(Error::ErrChunk.into());
2156+
return Err(Error::ErrChunk);
21582157
} else if let Some(c) = chunk_any.downcast_ref::<ChunkHeartbeat>() {
21592158
self.handle_heartbeat(c).await?
21602159
} else if let Some(c) = chunk_any.downcast_ref::<ChunkCookieEcho>() {
@@ -2176,7 +2175,7 @@ impl AssociationInternal {
21762175
} else if let Some(c) = chunk_any.downcast_ref::<ChunkShutdownComplete>() {
21772176
self.handle_shutdown_complete(c).await?
21782177
} else {
2179-
return Err(Error::ErrChunkTypeUnhandled.into());
2178+
return Err(Error::ErrChunkTypeUnhandled);
21802179
};
21812180

21822181
if !packets.is_empty() {

crates/sctp/src/association/association_internal/association_internal_test.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ use super::*;
22
use std::io;
33
use std::net::SocketAddr;
44

5+
type Result<T> = std::result::Result<T, util::Error>;
6+
7+
impl From<Error> for util::Error {
8+
fn from(e: Error) -> Self {
9+
util::Error::from_std(e)
10+
}
11+
}
12+
513
struct DumbConn;
614

715
#[async_trait]
@@ -451,17 +459,19 @@ async fn test_assoc_max_message_size_default() -> Result<()> {
451459
let ppi = PayloadProtocolIdentifier::from(s.default_payload_type.load(Ordering::SeqCst));
452460

453461
if let Err(err) = s.write_sctp(&p.slice(..65536), ppi).await {
454-
assert!(
455-
!Error::ErrOutboundPacketTooLarge.equal(&err),
462+
assert_ne!(
463+
Error::ErrOutboundPacketTooLarge,
464+
err,
456465
"should be not Error::ErrOutboundPacketTooLarge"
457466
);
458467
} else {
459468
assert!(false, "should be error");
460469
}
461470

462471
if let Err(err) = s.write_sctp(&p.slice(..65537), ppi).await {
463-
assert!(
464-
Error::ErrOutboundPacketTooLarge.equal(&err),
472+
assert_eq!(
473+
Error::ErrOutboundPacketTooLarge,
474+
err,
465475
"should be Error::ErrOutboundPacketTooLarge"
466476
);
467477
} else {
@@ -495,17 +505,19 @@ async fn test_assoc_max_message_size_explicit() -> Result<()> {
495505
let ppi = PayloadProtocolIdentifier::from(s.default_payload_type.load(Ordering::SeqCst));
496506

497507
if let Err(err) = s.write_sctp(&p.slice(..30000), ppi).await {
498-
assert!(
499-
!Error::ErrOutboundPacketTooLarge.equal(&err),
508+
assert_ne!(
509+
Error::ErrOutboundPacketTooLarge,
510+
err,
500511
"should be not Error::ErrOutboundPacketTooLarge"
501512
);
502513
} else {
503514
assert!(false, "should be error");
504515
}
505516

506517
if let Err(err) = s.write_sctp(&p.slice(..30001), ppi).await {
507-
assert!(
508-
Error::ErrOutboundPacketTooLarge.equal(&err),
518+
assert_eq!(
519+
Error::ErrOutboundPacketTooLarge,
520+
err,
509521
"should be Error::ErrOutboundPacketTooLarge"
510522
);
511523
} else {

0 commit comments

Comments
 (0)