Skip to content

Commit

Permalink
apply clippy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Aug 25, 2024
1 parent 5f125fb commit 9033d72
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 44 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ str-to-string = "warn"
use-debug = "warn"
iter-without-into-iter = "warn"
decimal-literal-representation = "warn"
ref-as-ptr = "warn"
8 changes: 3 additions & 5 deletions crates/client/input/src/keyboard_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ impl Input<KeyboardInput> {
pub fn get_movement_modifier(positive: bool, negative: bool) -> f32 {
if positive == negative {
0.0
} else if positive {
1.0
} else {
if positive {
1.0
} else {
-1.0
}
-1.0
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/client/option/src/callbacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait CyclingCallbacks<T, Txt>: Callbacks<T, Txt>
where
Txt: ProvideTextTy,
{
fn get_values(&self) -> (); // CyclingButtonWidget.Values<T>
fn get_values(&self); // CyclingButtonWidget.Values<T>

fn value_setter(&self) -> ValueSetter<T, Txt> {
|option, value| option.set_value(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl<T, Txt> CyclingCallbacks<T, Txt> for PotentialValuesBasedCallbacks<T>
where
Txt: ProvideTextTy,
{
fn get_values(&self) -> () {
fn get_values(&self) {
todo!()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<Txt> CyclingCallbacks<i32, Txt> for SuppliableIntCallbacks
where
Txt: ProvideTextTy,
{
fn get_values(&self) -> () {
fn get_values(&self) {
todo!()
}
}
Expand All @@ -36,14 +36,11 @@ where
}

fn i32_validate(&self, value: Option<i32>) -> Option<i32> {
match value {
Some(value) => Some(rimecraft_math::clamp(
value.map(|value| rimecraft_math::clamp(
value as f32,
<SuppliableIntCallbacks as IntSliderCallbacks<Txt>>::min_inclusive(self) as f32,
<SuppliableIntCallbacks as IntSliderCallbacks<Txt>>::max_inclusive(self) as f32,
) as i32),
None => None,
}
) as i32)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ where
}

fn i32_validate(&self, value: Option<i32>) -> Option<i32> {
match value {
Some(value) => {
if value >= self.min && value <= self.max {
Some(value)
} else {
None
}
}
None => None,
}
value.filter(|&value| value >= self.min && value <= self.max)
}
}

Expand Down
8 changes: 3 additions & 5 deletions crates/client/option/src/enums/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ pub trait ByUSizeId: Sequence {
if wraps {
let id = id % size;
Some(all.remove(id))
} else if id < size {
Some(all.remove(id))
} else {
if id < size {
Some(all.remove(id))
} else {
None
}
None
}
}
}
10 changes: 8 additions & 2 deletions crates/core/block-entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ where
pub unsafe fn downcast_ref<T>(&self) -> Option<&RawBlockEntity<'w, T, Cx>> {
if self.matches_type::<T>() {
unsafe {
Some(&*(self as *const BlockEntity<'w, Cx> as *const RawBlockEntity<'w, T, Cx>))
Some(
&*(std::ptr::from_ref::<BlockEntity<'w, Cx>>(self)
as *const RawBlockEntity<'w, T, Cx>),
)
}
} else {
None
Expand All @@ -325,7 +328,10 @@ where
pub unsafe fn downcast_mut<T>(&mut self) -> Option<&mut RawBlockEntity<'w, T, Cx>> {
if self.matches_type::<T>() {
unsafe {
Some(&mut *(self as *mut BlockEntity<'w, Cx> as *mut RawBlockEntity<'w, T, Cx>))
Some(
&mut *(std::ptr::from_mut::<BlockEntity<'w, Cx>>(self)
as *mut RawBlockEntity<'w, T, Cx>),
)
}
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion crates/core/block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,6 @@ where
{
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.block.hash(state);
(self.state as *const State<'_, Cx::BlockStateExt>).hash(state);
std::ptr::from_ref(self.state).hash(state);
}
}
6 changes: 4 additions & 2 deletions crates/core/component/src/dyn_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ impl<T: ?Sized> Any for T {}
impl dyn Any + Send + Sync + '_ {
pub unsafe fn downcast_ref<T>(&self) -> Option<&T> {
if (*self).type_id() == typeid::of::<T>() {
unsafe { Some(&*(self as *const dyn Any as *const T)) }
unsafe { Some(&*(std::ptr::from_ref::<dyn Any + Send + Sync + '_>(self) as *const T)) }
} else {
None
}
}

pub unsafe fn downcast_mut<T>(&mut self) -> Option<&mut T> {
if (*self).type_id() == typeid::of::<T>() {
unsafe { Some(&mut *(self as *mut dyn Any as *mut T)) }
unsafe {
Some(&mut *(std::ptr::from_mut::<dyn Any + Send + Sync + '_>(self) as *mut T))
}
} else {
None
}
Expand Down
24 changes: 15 additions & 9 deletions crates/core/component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ where
T: Clone + Eq + Hash + Send + Sync + 'a,
{
const UTIL: DynUtil<'a> = DynUtil {
clone: |obj| Box::new(unsafe { &*(obj as *const Object<'_> as *const T) }.clone()),
clone: |obj| {
Box::new(unsafe { &*(std::ptr::from_ref::<Object<'_>>(obj) as *const T) }.clone())
},
eq: |a, b| unsafe {
*(a as *const Object<'_> as *const T) == *(b as *const Object<'_> as *const T)
*(std::ptr::from_ref::<Object<'_>>(a) as *const T)
== *(std::ptr::from_ref::<Object<'_>>(b) as *const T)
},
hash: |obj, mut state| {
let obj = unsafe { &*(obj as *const Object<'_> as *const T) };
let obj = unsafe { &*(std::ptr::from_ref::<Object<'_>>(obj) as *const T) };
obj.hash(&mut state);
},
};
Expand All @@ -82,7 +85,9 @@ where
{
PacketCodec {
codec: UnsafePacketCodec {
encode: |obj, buf| unsafe { &*(obj as *const Object<'_> as *const T) }.encode(buf),
encode: |obj, buf| {
unsafe { &*(std::ptr::from_ref::<Object<'_>>(obj) as *const T) }.encode(buf)
},
decode: {
assert!(
<T as Decode<'_, _>>::SUPPORT_NON_IN_PLACE,
Expand All @@ -91,7 +96,8 @@ where
|buf| Ok(Box::new(T::decode(buf)?))
},
upd: |obj, buf| {
unsafe { &mut *(obj as *mut Object<'_> as *mut T) }.decode_in_place(buf)
unsafe { &mut *(std::ptr::from_mut::<Object<'_>>(obj) as *mut T) }
.decode_in_place(buf)
},
},
_marker: PhantomData,
Expand All @@ -108,15 +114,15 @@ where
codec: UnsafePacketCodec {
encode: |obj, buf| {
Cx::write_nbt(
unsafe { &*(obj as *const Object<'_> as *const T) },
unsafe { &*(std::ptr::from_ref::<Object<'_>>(obj) as *const T) },
buf.writer(),
)
.map_err(Into::into)
},
decode: |buf| Ok(Box::new(Cx::read_nbt(buf.reader())?)),
upd: |obj, buf| {
Cx::update_nbt(
unsafe { &mut *(obj as *mut Object<'_> as *mut T) },
unsafe { &mut *(std::ptr::from_mut::<Object<'_>>(obj) as *mut T) },
buf.reader(),
)
.map_err(Into::into)
Expand All @@ -134,7 +140,7 @@ where
SerdeCodec {
codec: UnsafeSerdeCodec {
ser: |obj| unsafe {
&*(obj as *const Object<'_> as *const T
&*(std::ptr::from_ref::<Object<'_>>(obj) as *const T
as *const (dyn erased_serde::Serialize + 'a))
},
de: |deserializer| {
Expand All @@ -144,7 +150,7 @@ where
})
},
upd: |obj, deserializer| {
*unsafe { &mut *(obj as *mut Object<'_> as *mut T) } =
*unsafe { &mut *(std::ptr::from_mut::<Object<'_>>(obj) as *mut T) } =
erased_serde::deserialize::<T>(deserializer)?;
Ok(())
},
Expand Down
4 changes: 2 additions & 2 deletions crates/util/downcast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<T: ?Sized> Downcast<T> {
#[inline]
pub unsafe fn downcast_ref<V: ToStatic>(&self) -> Option<&V> {
if self.is_safe::<V>() {
unsafe { Some(&*(&self.value as *const T as *const V)) }
unsafe { Some(&*(core::ptr::from_ref::<T>(&self.value) as *const V)) }
} else {
None
}
Expand All @@ -90,7 +90,7 @@ impl<T: ?Sized> Downcast<T> {
#[inline]
pub unsafe fn downcast_mut<V: ToStatic>(&mut self) -> Option<&mut V> {
if self.is_safe::<V>() {
unsafe { Some(&mut *(&mut self.value as *mut T as *mut V)) }
unsafe { Some(&mut *(core::ptr::from_mut::<T>(&mut self.value) as *mut V)) }
} else {
None
}
Expand Down

0 comments on commit 9033d72

Please sign in to comment.