Skip to content

Commit 8075c6b

Browse files
Common error types
1 parent 4484b46 commit 8075c6b

6 files changed

Lines changed: 78 additions & 73 deletions

File tree

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod prelude {
1010
pub use crate::stopwatch::Stopwatch;
1111
pub use crate::sync::bounded as local_bounded;
1212
pub use crate::sync::condvar as local_condvar;
13+
pub use crate::sync::error as local_sync_error;
1314
pub use crate::sync::oneshot as local_oneshot;
1415
pub use crate::sync::semaphore as local_semaphore;
1516
pub use crate::sync::unbounded as local_unbounded;

src/sync/bounded.rs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::shared::UnsafeShared;
2+
use crate::sync::error::{SendError, TrySendError};
23
use futures::Stream;
34
use std::cell::UnsafeCell;
45
use std::collections::VecDeque;
56
use std::rc::Rc;
67
use std::task::{Context, Poll, Waker};
7-
use std::{fmt, io};
88
use std::{future::poll_fn, pin::Pin};
99

1010
struct State<T> {
@@ -25,17 +25,6 @@ fn take_and_wake(waker: &mut Option<Waker>) {
2525
waker.take().inspect(Waker::wake_by_ref);
2626
}
2727

28-
#[derive(PartialEq, Eq)]
29-
pub enum TrySendError<T> {
30-
Full(T),
31-
Closed(T),
32-
}
33-
34-
#[derive(PartialEq, Eq)]
35-
pub enum SendError<T> {
36-
Closed(T),
37-
}
38-
3928
/// Bounded SPSC channel
4029
pub fn channel<T>(limit: usize) -> (Sender<T>, Receiver<T>) {
4130
let shared = Rc::new(UnsafeCell::new(State {
@@ -170,63 +159,6 @@ impl<T> Drop for Receiver<T> {
170159
}
171160
}
172161

173-
impl<T> fmt::Debug for TrySendError<T> {
174-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175-
match self {
176-
TrySendError::Full(_) => f.write_str("TrySendError::Full(..)"),
177-
TrySendError::Closed(_) => f.write_str("TrySendError::Closed(..)"),
178-
}
179-
}
180-
}
181-
182-
impl<T> fmt::Display for TrySendError<T> {
183-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184-
match self {
185-
TrySendError::Full(_) => f.write_str("channel is full"),
186-
TrySendError::Closed(_) => f.write_str("channel is closed"),
187-
}
188-
}
189-
}
190-
191-
impl<T> std::error::Error for TrySendError<T> {}
192-
193-
impl<T> From<TrySendError<T>> for io::Error {
194-
fn from(err: TrySendError<T>) -> Self {
195-
let source = format!("{err}");
196-
match err {
197-
TrySendError::Full(_) => io::Error::new(io::ErrorKind::StorageFull, source),
198-
TrySendError::Closed(_) => io::Error::new(io::ErrorKind::BrokenPipe, source),
199-
}
200-
}
201-
}
202-
203-
impl<T> fmt::Debug for SendError<T> {
204-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
205-
match self {
206-
SendError::Closed(_) => f.write_str("SendError::Closed(..)"),
207-
}
208-
}
209-
}
210-
211-
impl<T> fmt::Display for SendError<T> {
212-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213-
match self {
214-
SendError::Closed(_) => f.write_str("channel is closed"),
215-
}
216-
}
217-
}
218-
219-
impl<T> std::error::Error for SendError<T> {}
220-
221-
impl<T> From<SendError<T>> for io::Error {
222-
fn from(err: SendError<T>) -> Self {
223-
let source = format!("{err}");
224-
match err {
225-
SendError::Closed(_) => io::Error::new(io::ErrorKind::BrokenPipe, source),
226-
}
227-
}
228-
}
229-
230162
#[cfg(test)]
231163
mod tests {
232164
use super::*;

src/sync/error.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::{fmt, io};
2+
3+
#[derive(PartialEq, Eq)]
4+
pub enum TrySendError<T> {
5+
Full(T),
6+
Closed(T),
7+
}
8+
9+
#[derive(PartialEq, Eq)]
10+
pub enum SendError<T> {
11+
Closed(T),
12+
}
13+
14+
impl<T> fmt::Debug for TrySendError<T> {
15+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16+
match self {
17+
TrySendError::Full(_) => f.write_str("TrySendError::Full(..)"),
18+
TrySendError::Closed(_) => f.write_str("TrySendError::Closed(..)"),
19+
}
20+
}
21+
}
22+
23+
impl<T> fmt::Display for TrySendError<T> {
24+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25+
match self {
26+
TrySendError::Full(_) => f.write_str("channel is full"),
27+
TrySendError::Closed(_) => f.write_str("channel is closed"),
28+
}
29+
}
30+
}
31+
32+
impl<T> std::error::Error for TrySendError<T> {}
33+
34+
impl<T> From<TrySendError<T>> for io::Error {
35+
fn from(err: TrySendError<T>) -> Self {
36+
let source = format!("{err}");
37+
match err {
38+
TrySendError::Full(_) => io::Error::new(io::ErrorKind::StorageFull, source),
39+
TrySendError::Closed(_) => io::Error::new(io::ErrorKind::BrokenPipe, source),
40+
}
41+
}
42+
}
43+
44+
impl<T> fmt::Debug for SendError<T> {
45+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46+
match self {
47+
SendError::Closed(_) => f.write_str("SendError::Closed(..)"),
48+
}
49+
}
50+
}
51+
52+
impl<T> fmt::Display for SendError<T> {
53+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
match self {
55+
SendError::Closed(_) => f.write_str("channel is closed"),
56+
}
57+
}
58+
}
59+
60+
impl<T> std::error::Error for SendError<T> {}
61+
62+
impl<T> From<SendError<T>> for io::Error {
63+
fn from(err: SendError<T>) -> Self {
64+
let source = format!("{err}");
65+
match err {
66+
SendError::Closed(_) => io::Error::new(io::ErrorKind::BrokenPipe, source),
67+
}
68+
}
69+
}

src/sync/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod bounded;
22
pub mod condvar;
3+
pub mod error;
34
pub mod oneshot;
45
pub mod semaphore;
56
mod shared_state;

src/sync/oneshot.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::shared_state::{SharedState, Source};
2+
use crate::sync::error::SendError;
23
use std::cell::Cell;
34
use std::future::Future;
45
use std::ops::ControlFlow;
@@ -40,13 +41,13 @@ pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
4041
}
4142

4243
impl<T> Sender<T> {
43-
pub fn send(self, value: T) -> Result<(), T> {
44+
pub fn send(self, value: T) -> Result<(), SendError<T>> {
4445
if self.0.has_receiver.get() {
4546
self.0.value.set(Some(value));
4647
self.0.notify();
4748
Ok(())
4849
} else {
49-
Err(value)
50+
Err(SendError::Closed(value))
5051
}
5152
}
5253
}

src/sync/unbounded.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::shared_state::{SharedState, Source};
22
use crate::sealed;
3+
use crate::sync::error::SendError;
34
use std::cell::Cell;
45
use std::ops::ControlFlow;
56
use std::pin::Pin;
@@ -47,9 +48,9 @@ impl<T> Sender<T> {
4748
!self.0.has_receiver.get()
4849
}
4950

50-
pub fn send(&self, item: T) -> Result<(), T> {
51+
pub fn send(&self, item: T) -> Result<(), SendError<T>> {
5152
if self.is_closed() {
52-
Err(item)
53+
Err(SendError::Closed(item))
5354
} else {
5455
self.0.queue.push(item);
5556
self.0.notify();

0 commit comments

Comments
 (0)