Skip to content

Commit

Permalink
fix future clippy::ref_as_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
tguichaoua committed Feb 11, 2024
1 parent 73f2601 commit c0fb2d8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ map_flatten = "warn"

ptr_as_ptr = "warn"
ptr_cast_constness = "warn"
#TODO(rust 1.77): enable `ref_as_ptr`
# ref_as_ptr = "warn"

[workspace.lints.rust]
unsafe_op_in_unsafe_fn = "warn"
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
#[cfg(feature = "trace")]
use bevy_utils::tracing::Span;
use fixedbitset::FixedBitSet;
use std::{any::TypeId, borrow::Borrow, fmt, mem::MaybeUninit};
use std::{any::TypeId, borrow::Borrow, fmt, mem::MaybeUninit, ptr};

use super::{
NopWorldQuery, QueryBuilder, QueryData, QueryEntityError, QueryFilter, QueryManyIter,
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
>(
&self,
) -> &QueryState<NewD, NewF> {
&*(self as *const QueryState<D, F>).cast::<QueryState<NewD, NewF>>()
&*ptr::from_ref(self).cast::<QueryState<NewD, NewF>>()
}

/// Returns the archetype components accessed by this query.
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/world/unsafe_world_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
system::{Res, Resource},
};
use bevy_ptr::Ptr;
use std::{any::TypeId, cell::UnsafeCell, fmt::Debug, marker::PhantomData};
use std::{any::TypeId, cell::UnsafeCell, fmt::Debug, marker::PhantomData, ptr};

/// Variant of the [`World`] where resource and component accesses take `&self`, and the responsibility to avoid
/// aliasing violations are given to the caller instead of being checked at compile-time by rust's unique XOR shared rule.
Expand Down Expand Up @@ -85,13 +85,13 @@ impl<'w> UnsafeWorldCell<'w> {
/// Creates a [`UnsafeWorldCell`] that can be used to access everything immutably
#[inline]
pub(crate) fn new_readonly(world: &'w World) -> Self {
UnsafeWorldCell((world as *const World).cast_mut(), PhantomData)
Self(ptr::from_ref(world).cast_mut(), PhantomData)
}

/// Creates [`UnsafeWorldCell`] that can be used to access everything mutably
#[inline]
pub(crate) fn new_mutable(world: &'w mut World) -> Self {
Self(world as *mut World, PhantomData)
Self(ptr::from_mut(world), PhantomData)
}

/// Gets a mutable reference to the [`World`] this [`UnsafeWorldCell`] belongs to.
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_utils/src/synccell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! [`std::sync::Exclusive`]: https://doc.rust-lang.org/nightly/std/sync/struct.Exclusive.html
use std::ptr;

/// See [`Exclusive`](https://github.com/rust-lang/rust/issues/98407) for stdlib's upcoming implementation,
/// which should replace this one entirely.
///
Expand Down Expand Up @@ -41,7 +43,7 @@ impl<T: ?Sized> SyncCell<T> {
/// to its inner value, to skip constructing with [`new()`](SyncCell::new()).
pub fn from_mut(r: &'_ mut T) -> &'_ mut SyncCell<T> {
// SAFETY: repr is transparent, so refs have the same layout; and `SyncCell` properties are `&mut`-agnostic
unsafe { &mut *(r as *mut T as *mut SyncCell<T>) }
unsafe { &mut *(ptr::from_mut(r) as *mut SyncCell<T>) }
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_utils/src/syncunsafecell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! [`std::cell::SyncUnsafeCell`]: https://doc.rust-lang.org/nightly/std/cell/struct.SyncUnsafeCell.html
pub use core::cell::UnsafeCell;
use core::ptr;

/// [`UnsafeCell`], but [`Sync`].
///
Expand Down Expand Up @@ -78,7 +79,7 @@ impl<T: ?Sized> SyncUnsafeCell<T> {
#[inline]
/// Returns a `&mut SyncUnsafeCell<T>` from a `&mut T`.
pub fn from_mut(t: &mut T) -> &mut SyncUnsafeCell<T> {
let ptr = t as *mut T as *mut SyncUnsafeCell<T>;
let ptr = ptr::from_mut(t) as *mut SyncUnsafeCell<T>;
// SAFETY: `ptr` must be safe to mutably dereference, since it was originally
// obtained from a mutable reference. `SyncUnsafeCell` has the same representation
// as the original type `T`, since the former is annotated with #[repr(transparent)].
Expand All @@ -105,7 +106,7 @@ impl<T> SyncUnsafeCell<[T]> {
// - `SyncUnsafeCell<T>` has the same layout as `T`
// - `SyncUnsafeCell<[T]>` has the same layout as `[T]`
// - `SyncUnsafeCell<[T]>` has the same layout as `[SyncUnsafeCell<T>]`
unsafe { &*(self as *const SyncUnsafeCell<[T]> as *const [SyncUnsafeCell<T>]) }
unsafe { &*(ptr::from_ref(self) as *const [SyncUnsafeCell<T>]) }
}
}

Expand Down

0 comments on commit c0fb2d8

Please sign in to comment.