Skip to content

We may need a Send/Sync wrapper instead of NonNull<T> #16

@flier

Description

@flier

As you known, when we declare a foreign type that implements Send and Sync

foreign_type! {
    /// A Foo.
    pub unsafe type Foo
        : Sync + Send // optional
    {
        type CType = foo_sys::FOO;
        fn drop = foo_sys::FOO_free;
        fn clone = foo_sys::FOO_duplicate; // optional
    }
}

It will generate a type that wrap the NonNull<T>.

pub struct FooRef(Opaque);

unsafe impl ForeignTypeRef for FooRef {
    type CType = foo_sys::FOO;
}

pub struct Foo(NonNull<foo_sys::FOO>);

unsafe impl Sync for FooRef {}
unsafe impl Send for FooRef {}

unsafe impl Sync for Foo {}
unsafe impl Send for Foo {}

Then the nightly clippy will give a non_send_fields_in_send_ty warning, because NonNull<T> is !Send and !Sync.

   |
13 | / foreign_type! {
14 | |     /// A compiled pattern database that can then be used to scan data.
15 | |     pub unsafe type Database<T>: Send + Sync {
16 | |         type CType = ffi::hs_database_t;
...  |
20 | |     }
21 | | }
   | |_^
   |
   = note: `#[warn(clippy::non_send_fields_in_send_ty)]` on by default
note: the type of field `0` is `!Send`

We need a Send/Sync wrapper instead of NonNull<T>, maybe change to use UnsafeCell<NonNull<T>>?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions