-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
sync::broadcast: create Sender::new #5824
sync::broadcast: create Sender::new #5824
Conversation
I don't know how to fix the CI problem:
|
374bb1b
to
eed672b
Compare
eed672b
to
16cea8a
Compare
16cea8a
to
3b486b8
Compare
3b486b8
to
f5d2b5a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new_with_receiver_count
method is fine, but I have some comments.
}); | ||
|
||
pub fn channel<T: Clone>(capacity: usize) -> (Sender<T>, Receiver<T>) { | ||
let tx = unsafe { Sender::new_with_receiver_count(1, capacity) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two calls to this method should have a SAFETY comment that explains why the code is correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// SAFETY: In the line below we are creating one extra receiver, so there will be 1 in total.
and
// SAFETY: We don't create extra receivers, so there are 0.
#[track_caller] | ||
unsafe fn new_with_receiver_count(receiver_count: usize, mut capacity: usize) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is an unsafe method, it should have a comment that explains what the safety requirements are.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+ /// Creates the sending-half of the [`broadcast`] channel, and provide the receiver count.
+ ///
+ /// See the documentation of [`broadcast::channel`] for more errors when calling this
+ /// function.
+ ///
+ /// # Safety:
+ ///
+ /// The caller must ensure that the amount of receivers for this Sender is correct before
+ /// the channel functionalities are used, the count is zero by default, as this function
+ /// does not create any receivers by itself.
tokio/src/sync/broadcast.rs
Outdated
|
||
#[test] | ||
fn receiver_count_on_sender_constructor() { | ||
let count_of = |sender: &Sender<i32>| sender.shared.tail.lock().rx_cnt; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this to a real method since you're defining it twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I missed that Sender::receiver_count
is 100% equivalent to what I was doing.
Updated.
67ab20a
to
d769972
Compare
d769972
to
4b8d1cd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM.
Closes #4958