Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from dtolnay/sync
Require inventory element type to be Sync
- Loading branch information
Showing
4 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |