Skip to content

Commit bbe1b5e

Browse files
bors[bot]taiki-e
andauthored
Merge #637
637: Apply unreachable_pub lint r=jeehoonkang a=taiki-e `unreachable_pub` lint warns items that are marked as `pub` but are not actually included in the public API, and suggests using `pub(crate)` instead. `pub(crate)` is a bit more verbose than `pub`, but this should make it easier to review changes that make it difficult to tell if it will break API or not just by looking at the diff like #617 (comment). Especially, there are many such codes in crossbeam-channel and crossbeam-epoch. (Hopefully, after this patch, we can basically consider all items marked `pub` are public APIs, except for items that have been explicitly hidden from the public API by `#[doc(hidden)]`.) This lint is known to have some false positives, but the crossbeam doesn't seem to be affected. Co-authored-by: Taiki Endo <te316e89@gmail.com>
2 parents 92ca2da + 0541a37 commit bbe1b5e

File tree

28 files changed

+256
-209
lines changed

28 files changed

+256
-209
lines changed

crossbeam-channel/src/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ impl<T> SelectHandle for Receiver<T> {
14851485
}
14861486

14871487
/// Writes a message into the channel.
1488-
pub unsafe fn write<T>(s: &Sender<T>, token: &mut Token, msg: T) -> Result<(), T> {
1488+
pub(crate) unsafe fn write<T>(s: &Sender<T>, token: &mut Token, msg: T) -> Result<(), T> {
14891489
match &s.flavor {
14901490
SenderFlavor::Array(chan) => chan.write(token, msg),
14911491
SenderFlavor::List(chan) => chan.write(token, msg),
@@ -1494,7 +1494,7 @@ pub unsafe fn write<T>(s: &Sender<T>, token: &mut Token, msg: T) -> Result<(), T
14941494
}
14951495

14961496
/// Reads a message from the channel.
1497-
pub unsafe fn read<T>(r: &Receiver<T>, token: &mut Token) -> Result<T, ()> {
1497+
pub(crate) unsafe fn read<T>(r: &Receiver<T>, token: &mut Token) -> Result<T, ()> {
14981498
match &r.flavor {
14991499
ReceiverFlavor::Array(chan) => chan.read(token),
15001500
ReceiverFlavor::List(chan) => chan.read(token),

crossbeam-channel/src/counter.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Counter<C> {
2121
}
2222

2323
/// Wraps a channel into the reference counter.
24-
pub fn new<C>(chan: C) -> (Sender<C>, Receiver<C>) {
24+
pub(crate) fn new<C>(chan: C) -> (Sender<C>, Receiver<C>) {
2525
let counter = Box::into_raw(Box::new(Counter {
2626
senders: AtomicUsize::new(1),
2727
receivers: AtomicUsize::new(1),
@@ -34,7 +34,7 @@ pub fn new<C>(chan: C) -> (Sender<C>, Receiver<C>) {
3434
}
3535

3636
/// The sending side.
37-
pub struct Sender<C> {
37+
pub(crate) struct Sender<C> {
3838
counter: *mut Counter<C>,
3939
}
4040

@@ -45,7 +45,7 @@ impl<C> Sender<C> {
4545
}
4646

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

5151
// Cloning senders and calling `mem::forget` on the clones could potentially overflow the
@@ -63,7 +63,7 @@ impl<C> Sender<C> {
6363
/// Releases the sender reference.
6464
///
6565
/// Function `disconnect` will be called if this is the last sender reference.
66-
pub unsafe fn release<F: FnOnce(&C) -> bool>(&self, disconnect: F) {
66+
pub(crate) unsafe fn release<F: FnOnce(&C) -> bool>(&self, disconnect: F) {
6767
if self.counter().senders.fetch_sub(1, Ordering::AcqRel) == 1 {
6868
disconnect(&self.counter().chan);
6969

@@ -89,7 +89,7 @@ impl<C> PartialEq for Sender<C> {
8989
}
9090

9191
/// The receiving side.
92-
pub struct Receiver<C> {
92+
pub(crate) struct Receiver<C> {
9393
counter: *mut Counter<C>,
9494
}
9595

@@ -100,7 +100,7 @@ impl<C> Receiver<C> {
100100
}
101101

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

106106
// Cloning receivers and calling `mem::forget` on the clones could potentially overflow the
@@ -118,7 +118,7 @@ impl<C> Receiver<C> {
118118
/// Releases the receiver reference.
119119
///
120120
/// Function `disconnect` will be called if this is the last receiver reference.
121-
pub unsafe fn release<F: FnOnce(&C) -> bool>(&self, disconnect: F) {
121+
pub(crate) unsafe fn release<F: FnOnce(&C) -> bool>(&self, disconnect: F) {
122122
if self.counter().receivers.fetch_sub(1, Ordering::AcqRel) == 1 {
123123
disconnect(&self.counter().chan);
124124

crossbeam-channel/src/flavors/array.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Default for ArrayToken {
5252
}
5353

5454
/// Bounded channel based on a preallocated array.
55-
pub struct Channel<T> {
55+
pub(crate) struct Channel<T> {
5656
/// The head of the channel.
5757
///
5858
/// This value is a "stamp" consisting of an index into the buffer, a mark bit, and a lap, but
@@ -95,7 +95,7 @@ pub struct Channel<T> {
9595

9696
impl<T> Channel<T> {
9797
/// Creates a bounded channel of capacity `cap`.
98-
pub fn with_capacity(cap: usize) -> Self {
98+
pub(crate) fn with_capacity(cap: usize) -> Self {
9999
assert!(cap > 0, "capacity must be positive");
100100

101101
// Compute constants `mark_bit` and `one_lap`.
@@ -138,12 +138,12 @@ impl<T> Channel<T> {
138138
}
139139

140140
/// Returns a receiver handle to the channel.
141-
pub fn receiver(&self) -> Receiver<'_, T> {
141+
pub(crate) fn receiver(&self) -> Receiver<'_, T> {
142142
Receiver(self)
143143
}
144144

145145
/// Returns a sender handle to the channel.
146-
pub fn sender(&self) -> Sender<'_, T> {
146+
pub(crate) fn sender(&self) -> Sender<'_, T> {
147147
Sender(self)
148148
}
149149

@@ -219,7 +219,7 @@ impl<T> Channel<T> {
219219
}
220220

221221
/// Writes a message into the channel.
222-
pub unsafe fn write(&self, token: &mut Token, msg: T) -> Result<(), T> {
222+
pub(crate) unsafe fn write(&self, token: &mut Token, msg: T) -> Result<(), T> {
223223
// If there is no slot, the channel is disconnected.
224224
if token.array.slot.is_null() {
225225
return Err(msg);
@@ -309,7 +309,7 @@ impl<T> Channel<T> {
309309
}
310310

311311
/// Reads a message from the channel.
312-
pub unsafe fn read(&self, token: &mut Token) -> Result<T, ()> {
312+
pub(crate) unsafe fn read(&self, token: &mut Token) -> Result<T, ()> {
313313
if token.array.slot.is_null() {
314314
// The channel is disconnected.
315315
return Err(());
@@ -327,7 +327,7 @@ impl<T> Channel<T> {
327327
}
328328

329329
/// Attempts to send a message into the channel.
330-
pub fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
330+
pub(crate) fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
331331
let token = &mut Token::default();
332332
if self.start_send(token) {
333333
unsafe { self.write(token, msg).map_err(TrySendError::Disconnected) }
@@ -337,7 +337,11 @@ impl<T> Channel<T> {
337337
}
338338

339339
/// Sends a message into the channel.
340-
pub fn send(&self, msg: T, deadline: Option<Instant>) -> Result<(), SendTimeoutError<T>> {
340+
pub(crate) fn send(
341+
&self,
342+
msg: T,
343+
deadline: Option<Instant>,
344+
) -> Result<(), SendTimeoutError<T>> {
341345
let token = &mut Token::default();
342346
loop {
343347
// Try sending a message several times.
@@ -386,7 +390,7 @@ impl<T> Channel<T> {
386390
}
387391

388392
/// Attempts to receive a message without blocking.
389-
pub fn try_recv(&self) -> Result<T, TryRecvError> {
393+
pub(crate) fn try_recv(&self) -> Result<T, TryRecvError> {
390394
let token = &mut Token::default();
391395

392396
if self.start_recv(token) {
@@ -397,7 +401,7 @@ impl<T> Channel<T> {
397401
}
398402

399403
/// Receives a message from the channel.
400-
pub fn recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError> {
404+
pub(crate) fn recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError> {
401405
let token = &mut Token::default();
402406
loop {
403407
// Try receiving a message several times.
@@ -448,7 +452,7 @@ impl<T> Channel<T> {
448452
}
449453

450454
/// Returns the current number of messages inside the channel.
451-
pub fn len(&self) -> usize {
455+
pub(crate) fn len(&self) -> usize {
452456
loop {
453457
// Load the tail, then load the head.
454458
let tail = self.tail.load(Ordering::SeqCst);
@@ -473,14 +477,14 @@ impl<T> Channel<T> {
473477
}
474478

475479
/// Returns the capacity of the channel.
476-
pub fn capacity(&self) -> Option<usize> {
480+
pub(crate) fn capacity(&self) -> Option<usize> {
477481
Some(self.cap)
478482
}
479483

480484
/// Disconnects the channel and wakes up all blocked senders and receivers.
481485
///
482486
/// Returns `true` if this call disconnected the channel.
483-
pub fn disconnect(&self) -> bool {
487+
pub(crate) fn disconnect(&self) -> bool {
484488
let tail = self.tail.fetch_or(self.mark_bit, Ordering::SeqCst);
485489

486490
if tail & self.mark_bit == 0 {
@@ -493,12 +497,12 @@ impl<T> Channel<T> {
493497
}
494498

495499
/// Returns `true` if the channel is disconnected.
496-
pub fn is_disconnected(&self) -> bool {
500+
pub(crate) fn is_disconnected(&self) -> bool {
497501
self.tail.load(Ordering::SeqCst) & self.mark_bit != 0
498502
}
499503

500504
/// Returns `true` if the channel is empty.
501-
pub fn is_empty(&self) -> bool {
505+
pub(crate) fn is_empty(&self) -> bool {
502506
let head = self.head.load(Ordering::SeqCst);
503507
let tail = self.tail.load(Ordering::SeqCst);
504508

@@ -510,7 +514,7 @@ impl<T> Channel<T> {
510514
}
511515

512516
/// Returns `true` if the channel is full.
513-
pub fn is_full(&self) -> bool {
517+
pub(crate) fn is_full(&self) -> bool {
514518
let tail = self.tail.load(Ordering::SeqCst);
515519
let head = self.head.load(Ordering::SeqCst);
516520

@@ -558,10 +562,10 @@ impl<T> Drop for Channel<T> {
558562
}
559563

560564
/// Receiver handle to a channel.
561-
pub struct Receiver<'a, T>(&'a Channel<T>);
565+
pub(crate) struct Receiver<'a, T>(&'a Channel<T>);
562566

563567
/// Sender handle to a channel.
564-
pub struct Sender<'a, T>(&'a Channel<T>);
568+
pub(crate) struct Sender<'a, T>(&'a Channel<T>);
565569

566570
impl<T> SelectHandle for Receiver<'_, T> {
567571
fn try_select(&self, token: &mut Token) -> bool {

crossbeam-channel/src/flavors/at.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use crate::select::{Operation, SelectHandle, Token};
1212
use crate::utils;
1313

1414
/// Result of a receive operation.
15-
pub type AtToken = Option<Instant>;
15+
pub(crate) type AtToken = Option<Instant>;
1616

1717
/// Channel that delivers a message at a certain moment in time
18-
pub struct Channel {
18+
pub(crate) struct Channel {
1919
/// The instant at which the message will be delivered.
2020
delivery_time: Instant,
2121

@@ -26,21 +26,21 @@ pub struct Channel {
2626
impl Channel {
2727
/// Creates a channel that delivers a message at a certain instant in time.
2828
#[inline]
29-
pub fn new_deadline(when: Instant) -> Self {
29+
pub(crate) fn new_deadline(when: Instant) -> Self {
3030
Channel {
3131
delivery_time: when,
3232
received: AtomicBool::new(false),
3333
}
3434
}
3535
/// Creates a channel that delivers a message after a certain duration of time.
3636
#[inline]
37-
pub fn new_timeout(dur: Duration) -> Self {
37+
pub(crate) fn new_timeout(dur: Duration) -> Self {
3838
Self::new_deadline(Instant::now() + dur)
3939
}
4040

4141
/// Attempts to receive a message without blocking.
4242
#[inline]
43-
pub fn try_recv(&self) -> Result<Instant, TryRecvError> {
43+
pub(crate) fn try_recv(&self) -> Result<Instant, TryRecvError> {
4444
// We use relaxed ordering because this is just an optional optimistic check.
4545
if self.received.load(Ordering::Relaxed) {
4646
// The message has already been received.
@@ -64,7 +64,7 @@ impl Channel {
6464

6565
/// Receives a message from the channel.
6666
#[inline]
67-
pub fn recv(&self, deadline: Option<Instant>) -> Result<Instant, RecvTimeoutError> {
67+
pub(crate) fn recv(&self, deadline: Option<Instant>) -> Result<Instant, RecvTimeoutError> {
6868
// We use relaxed ordering because this is just an optional optimistic check.
6969
if self.received.load(Ordering::Relaxed) {
7070
// The message has already been received.
@@ -103,13 +103,13 @@ impl Channel {
103103

104104
/// Reads a message from the channel.
105105
#[inline]
106-
pub unsafe fn read(&self, token: &mut Token) -> Result<Instant, ()> {
106+
pub(crate) unsafe fn read(&self, token: &mut Token) -> Result<Instant, ()> {
107107
token.at.ok_or(())
108108
}
109109

110110
/// Returns `true` if the channel is empty.
111111
#[inline]
112-
pub fn is_empty(&self) -> bool {
112+
pub(crate) fn is_empty(&self) -> bool {
113113
// We use relaxed ordering because this is just an optional optimistic check.
114114
if self.received.load(Ordering::Relaxed) {
115115
return true;
@@ -127,13 +127,13 @@ impl Channel {
127127

128128
/// Returns `true` if the channel is full.
129129
#[inline]
130-
pub fn is_full(&self) -> bool {
130+
pub(crate) fn is_full(&self) -> bool {
131131
!self.is_empty()
132132
}
133133

134134
/// Returns the number of messages in the channel.
135135
#[inline]
136-
pub fn len(&self) -> usize {
136+
pub(crate) fn len(&self) -> usize {
137137
if self.is_empty() {
138138
0
139139
} else {
@@ -143,7 +143,7 @@ impl Channel {
143143

144144
/// Returns the capacity of the channel.
145145
#[inline]
146-
pub fn capacity(&self) -> Option<usize> {
146+
pub(crate) fn capacity(&self) -> Option<usize> {
147147
Some(1)
148148
}
149149
}

0 commit comments

Comments
 (0)