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

Require inventory element type to be Sync #42

Merged
merged 2 commits into from
Nov 10, 2021
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
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct Node<T: 'static> {
/// inventory::iter::<T>.into_iter().count()
/// }
/// ```
pub trait Collect: Sized + 'static {
pub trait Collect: Sync + Sized + 'static {
#[doc(hidden)]
fn registry() -> &'static Registry<Self>;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/collect-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ note: required because it appears within the type `Unsized`
note: required by a bound in `Collect`
--> src/lib.rs
|
| pub trait Collect: Sized + 'static {
| ^^^^^ required by this bound in `Collect`
| pub trait Collect: Sync + Sized + 'static {
| ^^^^^ required by this bound in `Collect`
= note: this error originates in the macro `inventory::collect` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `str` cannot be known at compilation time
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/non-sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::rc::Rc;
use std::thread;

struct Thing(Rc<i32>);

inventory::collect!(Thing);

fn clone_all() {
for thing in inventory::iter::<Thing> {
let _ = Rc::clone(&thing.0);
}
}

fn main() {
// It would be bad if this were allowed. These threads would race on the
// nonatomic reference counts.
let thread1 = thread::spawn(clone_all);
let thread2 = thread::spawn(clone_all);
thread1.join().unwrap();
thread2.join().unwrap();
}
18 changes: 18 additions & 0 deletions tests/ui/non-sync.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0277]: `Rc<i32>` cannot be shared between threads safely
--> tests/ui/non-sync.rs:6:1
|
6 | inventory::collect!(Thing);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rc<i32>` cannot be shared between threads safely
|
= help: within `Thing`, the trait `Sync` is not implemented for `Rc<i32>`
note: required because it appears within the type `Thing`
--> tests/ui/non-sync.rs:4:8
|
4 | struct Thing(Rc<i32>);
| ^^^^^
note: required by a bound in `Collect`
--> src/lib.rs
|
| pub trait Collect: Sync + Sized + 'static {
| ^^^^ required by this bound in `Collect`
= note: this error originates in the macro `inventory::collect` (in Nightly builds, run with -Z macro-backtrace for more info)