Skip to content

Commit b92d7e0

Browse files
committed
Fix clippy issues
1 parent 2ceb6a7 commit b92d7e0

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

crates/bevy_ecs/src/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl BundleInfo {
398398
column.initialize(
399399
table_row,
400400
component_ptr,
401-
ComponentTicks::new(change_tick.into()),
401+
ComponentTicks::new(change_tick),
402402
);
403403
}
404404
ComponentStatus::Mutated => {

crates/bevy_ecs/src/change_detection.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ use cfg_if::cfg_if;
2828
cfg_if! {
2929
if #[cfg(target_has_atomic = "64")] {
3030
type AtomicTick = core::sync::atomic::AtomicU64;
31+
type AtomicTickValue = u64;
3132
} else {
3233
type AtomicTick = core::sync::atomic::AtomicU32;
34+
type AtomicTickValue = u32;
3335
const ATOMIC_PANIC_THRESHOLD: u32 = u32::MAX;
3436
}
3537
}
@@ -51,9 +53,18 @@ impl Display for Tick {
5153
}
5254
}
5355

56+
impl Default for Tick {
57+
#[inline(always)]
58+
fn default() -> Self {
59+
Tick::new()
60+
}
61+
}
62+
5463
impl Tick {
5564
#[inline(always)]
56-
pub fn new() -> Self {
65+
pub const fn new() -> Self {
66+
// Safety:
67+
// `TICK_BASE_VALUE` is nonzero
5768
Tick(unsafe { NonZeroU64::new_unchecked(TICK_BASE_VALUE) })
5869
}
5970
}
@@ -77,9 +88,16 @@ impl Tick {
7788
#[derive(Clone, Copy, Debug)]
7889
pub struct SmallTick(pub u32);
7990

91+
impl Default for SmallTick {
92+
#[inline(always)]
93+
fn default() -> Self {
94+
SmallTick::new()
95+
}
96+
}
97+
8098
impl SmallTick {
8199
#[inline(always)]
82-
pub fn new() -> Self {
100+
pub const fn new() -> Self {
83101
SmallTick(0)
84102
}
85103
}
@@ -121,15 +139,15 @@ impl SmallTick {
121139
/// [`TickCounter::maintain`] returns `true`.
122140
#[inline(always)]
123141
pub fn maintain(&mut self, current: Tick) {
124-
let age = (current.0.get() as u32).wrapping_sub(self.0.into());
142+
let age = (current.0.get() as u32).wrapping_sub(self.0);
125143
if age > MAX_CHANGE_AGE {
126144
self.0 = (current.0.get() as u32).wrapping_sub(MAX_CHANGE_AGE);
127145
}
128146
}
129147

130148
#[cfg(test)]
131149
fn needs_maintenance(&self, current: Tick) -> bool {
132-
let age = (current.0.get() as u32).wrapping_sub(self.0.into());
150+
let age = (current.0.get() as u32).wrapping_sub(self.0);
133151
age > MAX_CHANGE_AGE
134152
}
135153
}
@@ -154,6 +172,14 @@ pub struct TickCounter {
154172
last_maintenance: u64,
155173
}
156174

175+
impl Default for TickCounter {
176+
#[inline(always)]
177+
fn default() -> Self {
178+
TickCounter::new()
179+
}
180+
}
181+
182+
// #[allow(clippy::useless_conversion)]
157183
impl TickCounter {
158184
#[inline]
159185
pub fn new() -> TickCounter {
@@ -181,10 +207,10 @@ impl TickCounter {
181207
pub fn maintain(&mut self) -> bool {
182208
#[cfg(not(target_has_atomic = "64"))]
183209
{
184-
self.offset += std::mem::replace(self.atomic.get_mut(), 0);
210+
self.offset += std::mem::replace(self.atomic.get_mut(), 0) as u64;
185211
}
186212

187-
let atomic = u64::from(*self.atomic.get_mut());
213+
let atomic = *self.atomic.get_mut();
188214
let tick_index = self.tick_index(atomic);
189215
if tick_index > self.last_maintenance + CHECK_TICK_THRESHOLD as u64 {
190216
self.last_maintenance = tick_index;
@@ -197,43 +223,41 @@ impl TickCounter {
197223
#[inline(always)]
198224
pub fn current(&self) -> Tick {
199225
let atomic = self.atomic.load(Relaxed);
200-
self.make_tick(atomic.into())
226+
self.make_tick(atomic)
201227
}
202228

203229
#[inline(always)]
204230
pub fn current_mut(&mut self) -> Tick {
205231
let atomic = *self.atomic.get_mut();
206-
self.make_tick(atomic.into())
232+
self.make_tick(atomic)
207233
}
208234

209235
#[inline(always)]
210236
pub fn next(&self) -> Tick {
211237
let atomic = self.atomic.fetch_add(1, Relaxed);
212-
self.make_tick(atomic.into())
238+
self.make_tick(atomic)
213239
}
214240

215241
#[inline]
216-
fn tick_index(&self, atomic: u64) -> u64 {
242+
fn tick_index(&self, atomic: AtomicTickValue) -> u64 {
217243
cfg_if! {
218244
if #[cfg(target_has_atomic = "64")] {
219245
atomic
220246
} else {
221-
if atomic >= ATOMIC_PANIC_THRESHOLD as u64 {
247+
if atomic >= ATOMIC_PANIC_THRESHOLD {
222248
panic!("Too many `TickCounter::next` calls between `TickCounter::maintain` calls")
223249
}
224-
self.offset + atomic
250+
self.offset + atomic as u64
225251
}
226252
}
227253
}
228254

229255
#[inline(always)]
230-
fn make_tick(&self, atomic: u64) -> Tick {
256+
fn make_tick(&self, atomic: AtomicTickValue) -> Tick {
231257
let index = self.tick_index(atomic);
232-
Tick(unsafe {
233-
// # Safety
234-
// `TICK_BASE_VALUE` is nonzero
235-
NonZeroU64::new(TICK_BASE_VALUE | index).unwrap_unchecked()
236-
})
258+
// Safety:
259+
// `TICK_BASE_VALUE` is nonzero
260+
Tick(unsafe { NonZeroU64::new(TICK_BASE_VALUE | index).unwrap_unchecked() })
237261
}
238262

239263
#[cfg(test)]

0 commit comments

Comments
 (0)