Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various cleanups #1045

Merged
merged 10 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name = "crossbeam"
# - Update README.md
# - Create "crossbeam-X.Y.Z" git tag
version = "0.8.2"
edition = "2018"
edition = "2021"
rust-version = "1.61"
license = "MIT OR Apache-2.0"
repository = "https://github.com/crossbeam-rs/crossbeam"
Expand Down Expand Up @@ -68,7 +68,11 @@ default-features = false
[dev-dependencies]
rand = "0.8"

[lints]
workspace = true

[workspace]
resolver = "2"
members = [
".",
"crossbeam-channel",
Expand All @@ -79,3 +83,9 @@ members = [
"crossbeam-skiplist",
"crossbeam-utils",
]

[workspace.lints.rust]
missing_debug_implementations = "warn"
rust_2018_idioms = "warn"
single_use_lifetimes = "warn"
unreachable_pub = "warn"
5 changes: 4 additions & 1 deletion crossbeam-channel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name = "crossbeam-channel"
# - Update README.md
# - Create "crossbeam-channel-X.Y.Z" git tag
version = "0.5.8"
edition = "2018"
edition = "2021"
rust-version = "1.61"
license = "MIT OR Apache-2.0"
repository = "https://github.com/crossbeam-rs/crossbeam"
Expand Down Expand Up @@ -36,3 +36,6 @@ optional = true
num_cpus = "1.13.0"
rand = "0.8"
signal-hook = "0.3"

[lints]
workspace = true
5 changes: 4 additions & 1 deletion crossbeam-channel/benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "benchmarks"
version = "0.1.0"
edition = "2018"
edition = "2021"
publish = false

[dependencies]
Expand Down Expand Up @@ -69,3 +69,6 @@ doc = false
name = "mpmc"
path = "mpmc.rs"
doc = false

[lints]
workspace = true
4 changes: 2 additions & 2 deletions crossbeam-channel/benchmarks/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
const LEN: usize = 1;

#[derive(Clone, Copy)]
pub struct Message(pub [usize; LEN]);
pub(crate) struct Message(pub(crate) [usize; LEN]);

impl fmt::Debug for Message {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -12,6 +12,6 @@ impl fmt::Debug for Message {
}

#[inline]
pub fn new(num: usize) -> Message {
pub(crate) fn new(num: usize) -> Message {
Message([num; LEN])
}
8 changes: 4 additions & 4 deletions crossbeam-channel/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ impl<T> Sender<T> {
/// let (s3, _) = unbounded();
/// assert!(!s.same_channel(&s3));
/// ```
pub fn same_channel(&self, other: &Sender<T>) -> bool {
pub fn same_channel(&self, other: &Self) -> bool {
match (&self.flavor, &other.flavor) {
(SenderFlavor::Array(ref a), SenderFlavor::Array(ref b)) => a == b,
(SenderFlavor::List(ref a), SenderFlavor::List(ref b)) => a == b,
Expand Down Expand Up @@ -678,7 +678,7 @@ impl<T> Clone for Sender<T> {
SenderFlavor::Zero(chan) => SenderFlavor::Zero(chan.acquire()),
};

Sender { flavor }
Self { flavor }
}
}

Expand Down Expand Up @@ -1142,7 +1142,7 @@ impl<T> Receiver<T> {
/// let (_, r3) = unbounded();
/// assert!(!r.same_channel(&r3));
/// ```
pub fn same_channel(&self, other: &Receiver<T>) -> bool {
pub fn same_channel(&self, other: &Self) -> bool {
match (&self.flavor, &other.flavor) {
(ReceiverFlavor::Array(a), ReceiverFlavor::Array(b)) => a == b,
(ReceiverFlavor::List(a), ReceiverFlavor::List(b)) => a == b,
Expand Down Expand Up @@ -1181,7 +1181,7 @@ impl<T> Clone for Receiver<T> {
ReceiverFlavor::Never(_) => ReceiverFlavor::Never(flavors::never::Channel::new()),
};

Receiver { flavor }
Self { flavor }
}
}

Expand Down
12 changes: 6 additions & 6 deletions crossbeam-channel/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,36 @@ impl Context {
#[inline]
pub fn with<F, R>(f: F) -> R
where
F: FnOnce(&Context) -> R,
F: FnOnce(&Self) -> R,
{
thread_local! {
/// Cached thread-local context.
static CONTEXT: Cell<Option<Context>> = Cell::new(Some(Context::new()));
}

let mut f = Some(f);
let mut f = |cx: &Context| -> R {
let mut f = |cx: &Self| -> R {
let f = f.take().unwrap();
f(cx)
};

CONTEXT
.try_with(|cell| match cell.take() {
None => f(&Context::new()),
None => f(&Self::new()),
Some(cx) => {
cx.reset();
let res = f(&cx);
cell.set(Some(cx));
res
}
})
.unwrap_or_else(|_| f(&Context::new()))
.unwrap_or_else(|_| f(&Self::new()))
}

/// Creates a new `Context`.
#[cold]
fn new() -> Context {
Context {
fn new() -> Self {
Self {
inner: Arc::new(Inner {
select: AtomicUsize::new(Selected::Waiting.into()),
packet: AtomicPtr::new(ptr::null_mut()),
Expand Down
12 changes: 6 additions & 6 deletions crossbeam-channel/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<C> Sender<C> {
}

/// Acquires another sender reference.
pub(crate) fn acquire(&self) -> Sender<C> {
pub(crate) fn acquire(&self) -> Self {
let count = self.counter().senders.fetch_add(1, Ordering::Relaxed);

// Cloning senders and calling `mem::forget` on the clones could potentially overflow the
Expand All @@ -55,7 +55,7 @@ impl<C> Sender<C> {
process::abort();
}

Sender {
Self {
counter: self.counter,
}
}
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<C> ops::Deref for Sender<C> {
}

impl<C> PartialEq for Sender<C> {
fn eq(&self, other: &Sender<C>) -> bool {
fn eq(&self, other: &Self) -> bool {
self.counter == other.counter
}
}
Expand All @@ -100,7 +100,7 @@ impl<C> Receiver<C> {
}

/// Acquires another receiver reference.
pub(crate) fn acquire(&self) -> Receiver<C> {
pub(crate) fn acquire(&self) -> Self {
let count = self.counter().receivers.fetch_add(1, Ordering::Relaxed);

// Cloning receivers and calling `mem::forget` on the clones could potentially overflow the
Expand All @@ -110,7 +110,7 @@ impl<C> Receiver<C> {
process::abort();
}

Receiver {
Self {
counter: self.counter,
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<C> ops::Deref for Receiver<C> {
}

impl<C> PartialEq for Receiver<C> {
fn eq(&self, other: &Receiver<C>) -> bool {
fn eq(&self, other: &Self) -> bool {
self.counter == other.counter
}
}
60 changes: 30 additions & 30 deletions crossbeam-channel/src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,27 @@ impl<T> SendError<T> {
impl<T> fmt::Debug for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
TrySendError::Full(..) => "Full(..)".fmt(f),
TrySendError::Disconnected(..) => "Disconnected(..)".fmt(f),
Self::Full(..) => "Full(..)".fmt(f),
Self::Disconnected(..) => "Disconnected(..)".fmt(f),
}
}
}

impl<T> fmt::Display for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
TrySendError::Full(..) => "sending on a full channel".fmt(f),
TrySendError::Disconnected(..) => "sending on a disconnected channel".fmt(f),
Self::Full(..) => "sending on a full channel".fmt(f),
Self::Disconnected(..) => "sending on a disconnected channel".fmt(f),
}
}
}

impl<T: Send> error::Error for TrySendError<T> {}

impl<T> From<SendError<T>> for TrySendError<T> {
fn from(err: SendError<T>) -> TrySendError<T> {
fn from(err: SendError<T>) -> Self {
match err {
SendError(t) => TrySendError::Disconnected(t),
SendError(t) => Self::Disconnected(t),
}
}
}
Expand All @@ -193,19 +193,19 @@ impl<T> TrySendError<T> {
/// ```
pub fn into_inner(self) -> T {
match self {
TrySendError::Full(v) => v,
TrySendError::Disconnected(v) => v,
Self::Full(v) => v,
Self::Disconnected(v) => v,
}
}

/// Returns `true` if the send operation failed because the channel is full.
pub fn is_full(&self) -> bool {
matches!(self, TrySendError::Full(_))
matches!(self, Self::Full(_))
}

/// Returns `true` if the send operation failed because the channel is disconnected.
pub fn is_disconnected(&self) -> bool {
matches!(self, TrySendError::Disconnected(_))
matches!(self, Self::Disconnected(_))
}
}

Expand All @@ -218,18 +218,18 @@ impl<T> fmt::Debug for SendTimeoutError<T> {
impl<T> fmt::Display for SendTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
SendTimeoutError::Timeout(..) => "timed out waiting on send operation".fmt(f),
SendTimeoutError::Disconnected(..) => "sending on a disconnected channel".fmt(f),
Self::Timeout(..) => "timed out waiting on send operation".fmt(f),
Self::Disconnected(..) => "sending on a disconnected channel".fmt(f),
}
}
}

impl<T: Send> error::Error for SendTimeoutError<T> {}

impl<T> From<SendError<T>> for SendTimeoutError<T> {
fn from(err: SendError<T>) -> SendTimeoutError<T> {
fn from(err: SendError<T>) -> Self {
match err {
SendError(e) => SendTimeoutError::Disconnected(e),
SendError(e) => Self::Disconnected(e),
}
}
}
Expand All @@ -251,19 +251,19 @@ impl<T> SendTimeoutError<T> {
/// ```
pub fn into_inner(self) -> T {
match self {
SendTimeoutError::Timeout(v) => v,
SendTimeoutError::Disconnected(v) => v,
Self::Timeout(v) => v,
Self::Disconnected(v) => v,
}
}

/// Returns `true` if the send operation timed out.
pub fn is_timeout(&self) -> bool {
matches!(self, SendTimeoutError::Timeout(_))
matches!(self, Self::Timeout(_))
}

/// Returns `true` if the send operation failed because the channel is disconnected.
pub fn is_disconnected(&self) -> bool {
matches!(self, SendTimeoutError::Disconnected(_))
matches!(self, Self::Disconnected(_))
}
}

Expand All @@ -278,62 +278,62 @@ impl error::Error for RecvError {}
impl fmt::Display for TryRecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
TryRecvError::Empty => "receiving on an empty channel".fmt(f),
TryRecvError::Disconnected => "receiving on an empty and disconnected channel".fmt(f),
Self::Empty => "receiving on an empty channel".fmt(f),
Self::Disconnected => "receiving on an empty and disconnected channel".fmt(f),
}
}
}

impl error::Error for TryRecvError {}

impl From<RecvError> for TryRecvError {
fn from(err: RecvError) -> TryRecvError {
fn from(err: RecvError) -> Self {
match err {
RecvError => TryRecvError::Disconnected,
RecvError => Self::Disconnected,
}
}
}

impl TryRecvError {
/// Returns `true` if the receive operation failed because the channel is empty.
pub fn is_empty(&self) -> bool {
matches!(self, TryRecvError::Empty)
matches!(self, Self::Empty)
}

/// Returns `true` if the receive operation failed because the channel is disconnected.
pub fn is_disconnected(&self) -> bool {
matches!(self, TryRecvError::Disconnected)
matches!(self, Self::Disconnected)
}
}

impl fmt::Display for RecvTimeoutError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
RecvTimeoutError::Timeout => "timed out waiting on receive operation".fmt(f),
RecvTimeoutError::Disconnected => "channel is empty and disconnected".fmt(f),
Self::Timeout => "timed out waiting on receive operation".fmt(f),
Self::Disconnected => "channel is empty and disconnected".fmt(f),
}
}
}

impl error::Error for RecvTimeoutError {}

impl From<RecvError> for RecvTimeoutError {
fn from(err: RecvError) -> RecvTimeoutError {
fn from(err: RecvError) -> Self {
match err {
RecvError => RecvTimeoutError::Disconnected,
RecvError => Self::Disconnected,
}
}
}

impl RecvTimeoutError {
/// Returns `true` if the receive operation timed out.
pub fn is_timeout(&self) -> bool {
matches!(self, RecvTimeoutError::Timeout)
matches!(self, Self::Timeout)
}

/// Returns `true` if the receive operation failed because the channel is disconnected.
pub fn is_disconnected(&self) -> bool {
matches!(self, RecvTimeoutError::Disconnected)
matches!(self, Self::Disconnected)
}
}

Expand Down
Loading