History

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.

Alice Ryhl committed 1 year ago

Commit 64fb8101 parent 8802e168 files · +15 −15Browse files

Unified · Split

8 files
  1. rust/kernel/configfs.rs +1 −1
  2. rust/kernel/init.rs +3 −3
  3. rust/kernel/lib.rs +2 −2
  4. rust/kernel/list.rs +1 −1
  5. rust/kernel/list/impl_list_item_mod.rs +2 −2
  6. rust/kernel/time/hrtimer.rs +1 −1
  7. rust/kernel/types.rs +4 −4
  8. rust/kernel/workqueue.rs +1 −1
rust/kernel/configfs.rs+1−1
@@ -279,7 +279,7 @@ impl<Data> Group<Data> { · 278 unmodified lines
279// within the `group` field.279// within the `group` field.
280unsafe impl<Data> HasGroup<Data> for Group<Data> {280unsafe impl<Data> HasGroup<Data> for Group<Data> {
281 unsafe fn group(this: *const Self) -> *const bindings::config_group {281 unsafe fn group(this: *const Self) -> *const bindings::config_group {
282 Opaque::raw_get(282 Opaque::cast_into(
283 // SAFETY: By impl and function safety requirements this field283 // SAFETY: By impl and function safety requirements this field
284 // projection is within bounds of the allocation.284 // projection is within bounds of the allocation.
285 unsafe { &raw const (*this).group },285 unsafe { &raw const (*this).group },
rust/kernel/init.rs+3−3
@@ -100,13 +100,13 @@ · 99 unmodified lines
100//! let foo = addr_of_mut!((*slot).foo);100//! let foo = addr_of_mut!((*slot).foo);
101//!101//!
102//! // Initialize the `foo`102//! // Initialize the `foo`
103//! bindings::init_foo(Opaque::raw_get(foo));103//! bindings::init_foo(Opaque::cast_into(foo));
104//!104//!
105//! // Try to enable it.105//! // 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);
107//! if err != 0 {107//! if err != 0 {
108//! // Enabling has failed, first clean up the foo and then return the error.108//! // 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));
110//! return Err(Error::from_errno(err));110//! return Err(Error::from_errno(err));
111//! }111//! }
112//!112//!
rust/kernel/lib.rs+2−2
@@ -204,11 +204,11 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! { · 203 unmodified lines
204 204
205/// Produces a pointer to an object from a pointer to one of its fields.205/// Produces a pointer to an object from a pointer to one of its fields.
206///206///
207/// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::raw_get`] or207/// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::cast_into`] or
208/// [`Opaque::cast_from`] to resolve the mismatch.208/// [`Opaque::cast_from`] to resolve the mismatch.
209///209///
210/// [`Opaque`]: crate::types::Opaque210/// [`Opaque`]: crate::types::Opaque
211/// [`Opaque::raw_get`]: crate::types::Opaque::raw_get211/// [`Opaque::cast_into`]: crate::types::Opaque::cast_into
212/// [`Opaque::cast_from`]: crate::types::Opaque::cast_from212/// [`Opaque::cast_from`]: crate::types::Opaque::cast_from
213///213///
214/// # Safety214/// # Safety
rust/kernel/list.rs+1−1
@@ -284,7 +284,7 @@ impl<const ID: u64> ListLinks<ID> { · 283 unmodified lines
284 #[inline]284 #[inline]
285 unsafe fn fields(me: *mut Self) -> *mut ListLinksFields {285 unsafe fn fields(me: *mut Self) -> *mut ListLinksFields {
286 // SAFETY: The caller promises that the pointer is valid.286 // 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)) }
288 }288 }
289 289
290 /// # Safety290 /// # Safety
rust/kernel/list/impl_list_item_mod.rs+2−2
@@ -209,7 +209,7 @@ macro_rules! impl_list_item { · 208 unmodified lines
209 // the pointer stays in bounds of the allocation.209 // the pointer stays in bounds of the allocation.
210 let self_ptr = unsafe { (links_field as *const u8).add(spoff) }210 let self_ptr = unsafe { (links_field as *const u8).add(spoff) }
211 as *const $crate::types::Opaque<*const Self>;211 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);
213 213
214 // SAFETY: This value is not accessed in any other places than `prepare_to_insert`,214 // SAFETY: This value is not accessed in any other places than `prepare_to_insert`,
215 // `post_remove`, or `view_value`. By the safety requirements of those methods,215 // `post_remove`, or `view_value`. By the safety requirements of those methods,
@@ -252,7 +252,7 @@ macro_rules! impl_list_item { · 36 unmodified lines
252 // the pointer stays in bounds of the allocation.252 // the pointer stays in bounds of the allocation.
253 let self_ptr = unsafe { (links_field as *const u8).add(spoff) }253 let self_ptr = unsafe { (links_field as *const u8).add(spoff) }
254 as *const ::core::cell::UnsafeCell<*const Self>;254 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);
256 // SAFETY: This is not a data race, because the only function that writes to this256 // SAFETY: This is not a data race, because the only function that writes to this
257 // value is `prepare_to_insert`, but by the safety requirements the257 // value is `prepare_to_insert`, but by the safety requirements the
258 // `prepare_to_insert` method may not be called in parallel with `view_value` or258 // `prepare_to_insert` method may not be called in parallel with `view_value` or
rust/kernel/time/hrtimer.rs+1−1
@@ -148,7 +148,7 @@ impl<T> HrTimer<T> { · 147 unmodified lines
148 // SAFETY: The field projection to `timer` does not go out of bounds,148 // SAFETY: The field projection to `timer` does not go out of bounds,
149 // because the caller of this function promises that `this` points to an149 // because the caller of this function promises that `this` points to an
150 // allocation of at least the size of `Self`.150 // 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)) }
152 }152 }
153 153
154 /// Cancel an initialized and potentially running timer.154 /// Cancel an initialized and potentially running timer.
rust/kernel/types.rs+4−4
@@ -377,7 +377,7 @@ impl<T> Opaque<T> { · 376 unmodified lines
377 // initialize the `T`.377 // initialize the `T`.
378 unsafe {378 unsafe {
379 pin_init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {379 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));
381 Ok(())381 Ok(())
382 })382 })
383 }383 }
@@ -397,7 +397,7 @@ impl<T> Opaque<T> { · 13 unmodified lines
397 // SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully397 // SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
398 // initialize the `T`.398 // initialize the `T`.
399 unsafe {399 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)))
401 }401 }
402 }402 }
403 403
@@ -410,11 +410,11 @@ impl<T> Opaque<T> { · 6 unmodified lines
410 ///410 ///
411 /// This function is useful to get access to the value without creating intermediate411 /// This function is useful to get access to the value without creating intermediate
412 /// references.412 /// references.
413 pub const fn raw_get(this: *const Self) -> *mut T {413 pub const fn cast_into(this: *const Self) -> *mut T {
414 UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()414 UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
415 }415 }
416 416
417 /// The opposite operation of [`Opaque::raw_get`].417 /// The opposite operation of [`Opaque::cast_into`].
418 pub const fn cast_from(this: *const T) -> *const Self {418 pub const fn cast_from(this: *const T) -> *const Self {
419 this.cast()419 this.cast()
420 }420 }
rust/kernel/workqueue.rs+1−1
@@ -403,7 +403,7 @@ impl<T: ?Sized, const ID: u64> Work<T, ID> { · 402 unmodified lines
403 //403 //
404 // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that404 // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
405 // the compiler does not complain that the `work` field is unused.405 // 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)) }
407 }407 }
408}408}
409 409