rustls / rustls-ffi

Use Rustls from any language
Other
124 stars 31 forks source link

usage of Arc<T> where T is not Send or Sync #333

Open jsha opened 1 year ago

jsha commented 1 year ago

Currently cargo clippy produces this error:

$ cargo clippy 
    Checking rustls-ffi v0.10.0 (/home/jsha/rust/rustls-ffi)
error: usage of `Arc<T>` where `T` is not `Send` or `Sync`
   --> src/lib.rs:396:23
    |
396 |         Arc::into_raw(Arc::new(src)) as *const _
    |                       ^^^^^^^^^^^^^
    |
    = help: consider using `Rc<T>` instead or wrapping `T` in a std::sync type like `Mutex<T>`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arc_with_non_send_sync
    = note: `#[deny(clippy::arc_with_non_send_sync)]` on by default

The relevant code is:

pub(crate) trait CastConstPtr {
    type RustType;

    fn cast_const_ptr(ptr: *const Self) -> *const Self::RustType {
        ptr as *const _
    }
}

pub(crate) trait CastConstPtr {
    type RustType;

    fn cast_const_ptr(ptr: *const Self) -> *const Self::RustType {
        ptr as *const _
    }
}

/// Anything that qualifies for CastPtr also automatically qualifies for
/// CastConstPtr. Splitting out CastPtr vs CastConstPtr allows us to ensure
/// that Arcs are never cast to a mutable pointer.
impl<T, R> CastConstPtr for T
where
    T: CastPtr<RustType = R>,
    R: Send,
{
    type RustType = R;
}

pub(crate) trait ArcCastPtr: CastConstPtr + Sized + Send + Sync {
    ...
    fn to_const_ptr(src: Self::RustType) -> *const Self {
        Arc::into_raw(Arc::new(src)) as *const _
    }

I think the clippy finding is a good one. Somewhere along the line we should probably be guaranteeing that the RustType for an ArcCastPtr is at least Send. I tried a couple of quick stabs at expressing this in the type system but haven't yet succeeded, so I'm filing this issue to track it.