Commit 0c7ae432 authored by Wedson Almeida Filho's avatar Wedson Almeida Filho Committed by Miguel Ojeda
Browse files

rust: types: implement `ForeignOwnable` for `Arc<T>`



This allows us to hand ownership of Rust ref-counted objects to
the C side of the kernel.

Signed-off-by: default avatarWedson Almeida Filho <wedsonaf@gmail.com>
Reviewed-by: default avatarGary Guo <gary@garyguo.net>
Reviewed-by: default avatarVincenzo Palazzo <vincenzopalazzodev@gmail.com>
Reviewed-by: default avatarAlice Ferrazzi <alice.ferrazzi@miraclelinux.com>
Reviewed-by: default avatarAndreas Hindborg <a.hindborg@samsung.com>
Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
parent 71185944
Loading
Loading
Loading
Loading
+31 −1
Original line number Diff line number Diff line
@@ -15,7 +15,11 @@
//!
//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html

use crate::{bindings, error::Result, types::Opaque};
use crate::{
    bindings,
    error::Result,
    types::{ForeignOwnable, Opaque},
};
use alloc::boxed::Box;
use core::{
    marker::{PhantomData, Unsize},
@@ -189,6 +193,32 @@ pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
    }
}

impl<T: 'static> ForeignOwnable for Arc<T> {
    type Borrowed<'a> = ArcBorrow<'a, T>;

    fn into_foreign(self) -> *const core::ffi::c_void {
        ManuallyDrop::new(self).ptr.as_ptr() as _
    }

    unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
        // SAFETY: By the safety requirement of this function, we know that `ptr` came from
        // a previous call to `Arc::into_foreign`.
        let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();

        // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
        // for the lifetime of the returned value. Additionally, the safety requirements of
        // `ForeignOwnable::borrow_mut` ensure that no new mutable references are created.
        unsafe { ArcBorrow::new(inner) }
    }

    unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
        // SAFETY: By the safety requirement of this function, we know that `ptr` came from
        // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
        // holds a reference count increment that is transferrable to us.
        unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) }
    }
}

impl<T: ?Sized> Deref for Arc<T> {
    type Target = T;