Commit 64fb810b authored by Alice Ryhl's avatar Alice Ryhl Committed by Miguel Ojeda
Browse files

rust: types: rename Opaque::raw_get to cast_into



In the previous patch we added Opaque::cast_from() that performs the
opposite operation to Opaque::raw_get(). For consistency with this
naming, rename raw_get() to cast_from().

There are a few other options such as calling cast_from() something
closer to raw_get() rather than renaming this method. However, I could
not find a great naming scheme that works with raw_get(). The previous
version of this patch used from_raw(), but functions of that name
typically have a different signature, so that's not a great option.

Suggested-by: default avatarDanilo Krummrich <dakr@kernel.org>
Signed-off-by: default avatarAlice Ryhl <aliceryhl@google.com>
Acked-by: default avatarBenno Lossin <lossin@kernel.org>
Acked-by: default avatarAndreas Hindborg <a.hindborg@kernel.org>
Acked-by: default avatarBoqun Feng <boqun.feng@gmail.com>
Reviewed-by: default avatarDanilo Krummrich <dakr@kernel.org>
Acked-by: default avatarDanilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/r/20250624-opaque-from-raw-v2-2-e4da40bdc59c@google.com


[ Removed `HrTimer::raw_get` change. - Miguel ]
Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
parent 8802e168
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -279,7 +279,7 @@ pub fn new(
// within the `group` field.
unsafe impl<Data> HasGroup<Data> for Group<Data> {
    unsafe fn group(this: *const Self) -> *const bindings::config_group {
        Opaque::raw_get(
        Opaque::cast_into(
            // SAFETY: By impl and function safety requirements this field
            // projection is within bounds of the allocation.
            unsafe { &raw const (*this).group },
+3 −3
Original line number Diff line number Diff line
@@ -100,13 +100,13 @@
//!                 let foo = addr_of_mut!((*slot).foo);
//!
//!                 // Initialize the `foo`
//!                 bindings::init_foo(Opaque::raw_get(foo));
//!                 bindings::init_foo(Opaque::cast_into(foo));
//!
//!                 // Try to enable it.
//!                 let err = bindings::enable_foo(Opaque::raw_get(foo), flags);
//!                 let err = bindings::enable_foo(Opaque::cast_into(foo), flags);
//!                 if err != 0 {
//!                     // Enabling has failed, first clean up the foo and then return the error.
//!                     bindings::destroy_foo(Opaque::raw_get(foo));
//!                     bindings::destroy_foo(Opaque::cast_into(foo));
//!                     return Err(Error::from_errno(err));
//!                 }
//!
+2 −2
Original line number Diff line number Diff line
@@ -204,11 +204,11 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {

/// Produces a pointer to an object from a pointer to one of its fields.
///
/// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::raw_get`] or
/// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::cast_into`] or
/// [`Opaque::cast_from`] to resolve the mismatch.
///
/// [`Opaque`]: crate::types::Opaque
/// [`Opaque::raw_get`]: crate::types::Opaque::raw_get
/// [`Opaque::cast_into`]: crate::types::Opaque::cast_into
/// [`Opaque::cast_from`]: crate::types::Opaque::cast_from
///
/// # Safety
+1 −1
Original line number Diff line number Diff line
@@ -284,7 +284,7 @@ pub fn new() -> impl PinInit<Self> {
    #[inline]
    unsafe fn fields(me: *mut Self) -> *mut ListLinksFields {
        // SAFETY: The caller promises that the pointer is valid.
        unsafe { Opaque::raw_get(ptr::addr_of!((*me).inner)) }
        unsafe { Opaque::cast_into(ptr::addr_of!((*me).inner)) }
    }

    /// # Safety
+2 −2
Original line number Diff line number Diff line
@@ -209,7 +209,7 @@ unsafe fn prepare_to_insert(me: *const Self) -> *mut $crate::list::ListLinks<$nu
                // the pointer stays in bounds of the allocation.
                let self_ptr = unsafe { (links_field as *const u8).add(spoff) }
                    as *const $crate::types::Opaque<*const Self>;
                let cell_inner = $crate::types::Opaque::raw_get(self_ptr);
                let cell_inner = $crate::types::Opaque::cast_into(self_ptr);

                // SAFETY: This value is not accessed in any other places than `prepare_to_insert`,
                // `post_remove`, or `view_value`. By the safety requirements of those methods,
@@ -252,7 +252,7 @@ unsafe fn view_value(links_field: *mut $crate::list::ListLinks<$num>) -> *const
                // the pointer stays in bounds of the allocation.
                let self_ptr = unsafe { (links_field as *const u8).add(spoff) }
                    as *const ::core::cell::UnsafeCell<*const Self>;
                let cell_inner = ::core::cell::UnsafeCell::raw_get(self_ptr);
                let cell_inner = ::core::cell::UnsafeCell::cast_into(self_ptr);
                // SAFETY: This is not a data race, because the only function that writes to this
                // value is `prepare_to_insert`, but by the safety requirements the
                // `prepare_to_insert` method may not be called in parallel with `view_value` or
Loading