Skip to content

Commit 64fb810

Browse files
Darksonnojeda
authored andcommitted
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: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Benno Lossin <lossin@kernel.org> Acked-by: Andreas Hindborg <a.hindborg@kernel.org> Acked-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Acked-by: Danilo 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: Miguel Ojeda <ojeda@kernel.org>
1 parent 8802e16 commit 64fb810

8 files changed

Lines changed: 15 additions & 15 deletions

File tree

rust/kernel/configfs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<Data> Group<Data> {
279279
// within the `group` field.
280280
unsafe impl<Data> HasGroup<Data> for Group<Data> {
281281
unsafe fn group(this: *const Self) -> *const bindings::config_group {
282-
Opaque::raw_get(
282+
Opaque::cast_into(
283283
// SAFETY: By impl and function safety requirements this field
284284
// projection is within bounds of the allocation.
285285
unsafe { &raw const (*this).group },

rust/kernel/init.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@
100100
//! let foo = addr_of_mut!((*slot).foo);
101101
//!
102102
//! // Initialize the `foo`
103-
//! bindings::init_foo(Opaque::raw_get(foo));
103+
//! bindings::init_foo(Opaque::cast_into(foo));
104104
//!
105105
//! // Try to enable it.
106-
//! let err = bindings::enable_foo(Opaque::raw_get(foo), flags);
106+
//! let err = bindings::enable_foo(Opaque::cast_into(foo), flags);
107107
//! if err != 0 {
108108
//! // Enabling has failed, first clean up the foo and then return the error.
109-
//! bindings::destroy_foo(Opaque::raw_get(foo));
109+
//! bindings::destroy_foo(Opaque::cast_into(foo));
110110
//! return Err(Error::from_errno(err));
111111
//! }
112112
//!

rust/kernel/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
204204

205205
/// Produces a pointer to an object from a pointer to one of its fields.
206206
///
207-
/// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::raw_get`] or
207+
/// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::cast_into`] or
208208
/// [`Opaque::cast_from`] to resolve the mismatch.
209209
///
210210
/// [`Opaque`]: crate::types::Opaque
211-
/// [`Opaque::raw_get`]: crate::types::Opaque::raw_get
211+
/// [`Opaque::cast_into`]: crate::types::Opaque::cast_into
212212
/// [`Opaque::cast_from`]: crate::types::Opaque::cast_from
213213
///
214214
/// # Safety

rust/kernel/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<const ID: u64> ListLinks<ID> {
284284
#[inline]
285285
unsafe fn fields(me: *mut Self) -> *mut ListLinksFields {
286286
// SAFETY: The caller promises that the pointer is valid.
287-
unsafe { Opaque::raw_get(ptr::addr_of!((*me).inner)) }
287+
unsafe { Opaque::cast_into(ptr::addr_of!((*me).inner)) }
288288
}
289289

290290
/// # Safety

rust/kernel/list/impl_list_item_mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ macro_rules! impl_list_item {
209209
// the pointer stays in bounds of the allocation.
210210
let self_ptr = unsafe { (links_field as *const u8).add(spoff) }
211211
as *const $crate::types::Opaque<*const Self>;
212-
let cell_inner = $crate::types::Opaque::raw_get(self_ptr);
212+
let cell_inner = $crate::types::Opaque::cast_into(self_ptr);
213213

214214
// SAFETY: This value is not accessed in any other places than `prepare_to_insert`,
215215
// `post_remove`, or `view_value`. By the safety requirements of those methods,
@@ -252,7 +252,7 @@ macro_rules! impl_list_item {
252252
// the pointer stays in bounds of the allocation.
253253
let self_ptr = unsafe { (links_field as *const u8).add(spoff) }
254254
as *const ::core::cell::UnsafeCell<*const Self>;
255-
let cell_inner = ::core::cell::UnsafeCell::raw_get(self_ptr);
255+
let cell_inner = ::core::cell::UnsafeCell::cast_into(self_ptr);
256256
// SAFETY: This is not a data race, because the only function that writes to this
257257
// value is `prepare_to_insert`, but by the safety requirements the
258258
// `prepare_to_insert` method may not be called in parallel with `view_value` or

rust/kernel/time/hrtimer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<T> HrTimer<T> {
148148
// SAFETY: The field projection to `timer` does not go out of bounds,
149149
// because the caller of this function promises that `this` points to an
150150
// allocation of at least the size of `Self`.
151-
unsafe { Opaque::raw_get(core::ptr::addr_of!((*this).timer)) }
151+
unsafe { Opaque::cast_into(core::ptr::addr_of!((*this).timer)) }
152152
}
153153

154154
/// Cancel an initialized and potentially running timer.

rust/kernel/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<T> Opaque<T> {
377377
// initialize the `T`.
378378
unsafe {
379379
pin_init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
380-
init_func(Self::raw_get(slot));
380+
init_func(Self::cast_into(slot));
381381
Ok(())
382382
})
383383
}
@@ -397,7 +397,7 @@ impl<T> Opaque<T> {
397397
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
398398
// initialize the `T`.
399399
unsafe {
400-
pin_init::pin_init_from_closure::<_, E>(move |slot| init_func(Self::raw_get(slot)))
400+
pin_init::pin_init_from_closure::<_, E>(move |slot| init_func(Self::cast_into(slot)))
401401
}
402402
}
403403

@@ -410,11 +410,11 @@ impl<T> Opaque<T> {
410410
///
411411
/// This function is useful to get access to the value without creating intermediate
412412
/// references.
413-
pub const fn raw_get(this: *const Self) -> *mut T {
413+
pub const fn cast_into(this: *const Self) -> *mut T {
414414
UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
415415
}
416416

417-
/// The opposite operation of [`Opaque::raw_get`].
417+
/// The opposite operation of [`Opaque::cast_into`].
418418
pub const fn cast_from(this: *const T) -> *const Self {
419419
this.cast()
420420
}

rust/kernel/workqueue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ impl<T: ?Sized, const ID: u64> Work<T, ID> {
403403
//
404404
// A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
405405
// the compiler does not complain that the `work` field is unused.
406-
unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) }
406+
unsafe { Opaque::cast_into(core::ptr::addr_of!((*ptr).work)) }
407407
}
408408
}
409409

0 commit comments

Comments
 (0)