Skip to content

Commit aa991a2

Browse files
tamirdojeda
authored andcommitted
rust: types: avoid as casts
Replace `as` casts with `cast{,_mut}` calls which are a bit safer. In one instance, remove an unnecessary `as` cast without replacement. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20241120-borrow-mut-v6-2-80dbadd00951@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent c6340da commit aa991a2

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

rust/kernel/alloc/kbox.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,13 @@ where
356356
type Borrowed<'a> = &'a T;
357357

358358
fn into_foreign(self) -> *const crate::ffi::c_void {
359-
Box::into_raw(self) as _
359+
Box::into_raw(self).cast()
360360
}
361361

362362
unsafe fn from_foreign(ptr: *const crate::ffi::c_void) -> Self {
363363
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
364364
// call to `Self::into_foreign`.
365-
unsafe { Box::from_raw(ptr as _) }
365+
unsafe { Box::from_raw(ptr.cast_mut().cast()) }
366366
}
367367

368368
unsafe fn borrow<'a>(ptr: *const crate::ffi::c_void) -> &'a T {
@@ -380,13 +380,13 @@ where
380380

381381
fn into_foreign(self) -> *const crate::ffi::c_void {
382382
// SAFETY: We are still treating the box as pinned.
383-
Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }) as _
383+
Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }).cast()
384384
}
385385

386386
unsafe fn from_foreign(ptr: *const crate::ffi::c_void) -> Self {
387387
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
388388
// call to `Self::into_foreign`.
389-
unsafe { Pin::new_unchecked(Box::from_raw(ptr as _)) }
389+
unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast_mut().cast())) }
390390
}
391391

392392
unsafe fn borrow<'a>(ptr: *const crate::ffi::c_void) -> Pin<&'a T> {

rust/kernel/sync/arc.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ impl<T> Arc<T> {
213213
};
214214

215215
let inner = KBox::new(value, flags)?;
216+
let inner = KBox::leak(inner).into();
216217

217218
// SAFETY: We just created `inner` with a reference count of 1, which is owned by the new
218219
// `Arc` object.
219-
Ok(unsafe { Self::from_inner(KBox::leak(inner).into()) })
220+
Ok(unsafe { Self::from_inner(inner) })
220221
}
221222
}
222223

@@ -345,13 +346,13 @@ impl<T: 'static> ForeignOwnable for Arc<T> {
345346
type Borrowed<'a> = ArcBorrow<'a, T>;
346347

347348
fn into_foreign(self) -> *const crate::ffi::c_void {
348-
ManuallyDrop::new(self).ptr.as_ptr() as _
349+
ManuallyDrop::new(self).ptr.as_ptr().cast()
349350
}
350351

351352
unsafe fn borrow<'a>(ptr: *const crate::ffi::c_void) -> ArcBorrow<'a, T> {
352353
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
353354
// call to `Self::into_foreign`.
354-
let inner = unsafe { NonNull::new_unchecked(ptr as *mut ArcInner<T>) };
355+
let inner = unsafe { NonNull::new_unchecked(ptr.cast_mut().cast::<ArcInner<T>>()) };
355356

356357
// SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
357358
// for the lifetime of the returned value.
@@ -361,7 +362,7 @@ impl<T: 'static> ForeignOwnable for Arc<T> {
361362
unsafe fn from_foreign(ptr: *const crate::ffi::c_void) -> Self {
362363
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
363364
// call to `Self::into_foreign`.
364-
let inner = unsafe { NonNull::new_unchecked(ptr as *mut ArcInner<T>) };
365+
let inner = unsafe { NonNull::new_unchecked(ptr.cast_mut().cast::<ArcInner<T>>()) };
365366

366367
// SAFETY: By the safety requirement of this function, we know that `ptr` came from
367368
// a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and

rust/kernel/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<T: AlwaysRefCounted> ARef<T> {
434434
/// }
435435
///
436436
/// let mut data = Empty {};
437-
/// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap();
437+
/// let ptr = NonNull::<Empty>::new(&mut data).unwrap();
438438
/// # // SAFETY: TODO.
439439
/// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
440440
/// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref);

0 commit comments

Comments
 (0)