Why I cannot define a Simple Struct with Safe + Send features enabled

  Kiến thức lập trình

This could be a very basic Rust question, but I am not understanding why I cannot create a Lazy construct for my simple struct:
pub struct S { a_: String, } impl S { pub fn new(a: String) -> Self { S { a_: a } } pub fn foo(&self) { println!("{}", self.a_); } } unsafe impl Sync for S {} unsafe impl Send for S {}

I am getting errors like:

error[E0277]: Cell<Option<fn() -> S>>cannot be shared between threads safely --> src/main.rs:26:15 | 26 | static X: Lazy<S> = Lazy::new(|| { | ^^^^^^^Cell<Option<fn() -> S>>cannot be shared between threads safely | = help: withinonce_cell::unsync::Lazy, the trait Syncis not implemented forCell<Option<fn() -> S>>= note: if you want to do aliasing and mutation between multiple threads, usestd::sync::RwLocknote: required because it appears within the typeLazy --> /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/once_cell-1.19.0/src/lib.rs:714:16 | 714 | pub struct Lazy<T, F = fn() -> T> { | ^^^^ = note: shared static variables must have a type that implementsSync“

First I tried to define the struct S without the custom impl’s:

unsafe impl Sync for S {}
unsafe impl Send for S {}

But it didn’t work. As in the below code I am adding them back but no results.


fn main() {
    let s = s_mod::S::new("10".to_string());
    s.foo();
}

mod s_mod {
    use once_cell::unsync::Lazy;

    pub struct S {
        a_: String,
    }
    impl S {
        pub fn new(a: String) -> Self {
            S {
                a_: a
            }
        }
        pub fn foo(&self) {
            println!("{}", self.a_);
        }
    }
    unsafe impl Sync for S {}
    unsafe impl Send for S {}
    
    static X: Lazy<S> = Lazy::new(|| {
       let s = S::new("4".to_string());
       s
    });
}

What am I missing here? Can someone help me understand?

LEAVE A COMMENT