dtolnay / inventory

Typed distributed plugin registration
Apache License 2.0
986 stars 43 forks source link

Require inventory element type to be Sync #42

Closed dtolnay closed 2 years ago

dtolnay commented 2 years ago

We hand out &'static T references onto arbitrary threads when iterating the inventory, so T: Sync is necessary.

use std::cell::Cell;
use std::thread;

struct Thing {
    cell: Cell<String>,
}

inventory::collect!(Thing);

inventory::submit! {
    Thing {
        cell: Cell::new(String::new()),
    }
}

fn main() {
    thread::spawn(move || {
        let thing = inventory::iter::<Thing>().next().unwrap();
        loop {
            thing.cell.set(".".repeat(40));
        }
    });

    let thing = inventory::iter::<Thing>().next().unwrap();
    loop {
        thing.cell.set(String::new());
        let _ = thing.cell.take().clone();
    }
}
double free or corruption (fasttop)
Aborted (core dumped)