Skip to content

Commit 8f1a221

Browse files
joseph-giogeieredgar
authored andcommitted
merge StaticRef with Leak
1 parent 3588700 commit 8f1a221

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

crates/bevy_ecs/src/schedule/set.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::marker::PhantomData;
55

66
pub use bevy_ecs_macros::{ScheduleLabel, SystemSet};
77
use bevy_utils::define_label;
8-
use bevy_utils::intern::{Interned, Interner, Leak, StaticRef};
8+
use bevy_utils::intern::{Interned, Interner, Leak};
99
use bevy_utils::label::DynHash;
1010

1111
use crate::system::{
@@ -100,9 +100,7 @@ impl Leak for dyn SystemSet {
100100
fn leak(&self) -> &'static Self {
101101
Box::leak(self.dyn_clone())
102102
}
103-
}
104103

105-
impl StaticRef for dyn SystemSet {
106104
fn static_ref(&self) -> Option<&'static dyn SystemSet> {
107105
self.dyn_static_ref()
108106
}

crates/bevy_utils/src/intern.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,7 @@ impl<T> From<&Interned<T>> for Interned<T> {
7979
pub trait Leak {
8080
/// Creates a static reference to `self`, possibly leaking memory.
8181
fn leak(&self) -> &'static Self;
82-
}
83-
84-
impl Leak for str {
85-
fn leak(&self) -> &'static Self {
86-
let str = self.to_owned().into_boxed_str();
87-
Box::leak(str)
88-
}
89-
}
9082

91-
/// A type that can provide static references to equal values.
92-
pub trait StaticRef {
9383
/// Returns a static reference to a value equal to `self`, if possible.
9484
/// This method is used by [`Interner::intern`] to optimize the interning process.
9585
///
@@ -127,7 +117,12 @@ pub trait StaticRef {
127117
}
128118
}
129119

130-
impl StaticRef for str {}
120+
impl Leak for str {
121+
fn leak(&self) -> &'static Self {
122+
let str = self.to_owned().into_boxed_str();
123+
Box::leak(str)
124+
}
125+
}
131126

132127
/// A thread-safe interner which can be used to create [`Interned<T>`] from `&T`
133128
///
@@ -145,7 +140,7 @@ impl<T: ?Sized> Interner<T> {
145140
}
146141
}
147142

148-
impl<T: Leak + StaticRef + Hash + Eq + ?Sized> Interner<T> {
143+
impl<T: Leak + Hash + Eq + ?Sized> Interner<T> {
149144
/// Return the [`Interned<T>`] corresponding to `value`.
150145
///
151146
/// If it is called the first time for `value`, it will possibly leak the value and return an
@@ -190,23 +185,21 @@ mod tests {
190185
hash::{Hash, Hasher},
191186
};
192187

193-
use crate::intern::{Interned, Interner, Leak, StaticRef};
188+
use crate::intern::{Interned, Interner, Leak};
194189

195190
#[test]
196191
fn zero_sized_type() {
197192
#[derive(PartialEq, Eq, Hash, Debug)]
198193
pub struct A;
199194

200-
impl StaticRef for A {
201-
fn static_ref(&self) -> Option<&'static Self> {
202-
Some(&A)
203-
}
204-
}
205-
206195
impl Leak for A {
207196
fn leak(&self) -> &'static Self {
208197
&A
209198
}
199+
200+
fn static_ref(&self) -> Option<&'static Self> {
201+
Some(&A)
202+
}
210203
}
211204

212205
let interner = Interner::default();
@@ -223,22 +216,20 @@ mod tests {
223216
Y,
224217
}
225218

226-
impl StaticRef for A {
227-
fn static_ref(&self) -> Option<&'static Self> {
228-
Some(match self {
229-
A::X => &A::X,
230-
A::Y => &A::Y,
231-
})
232-
}
233-
}
234-
235219
impl Leak for A {
236220
fn leak(&self) -> &'static Self {
237221
match self {
238222
A::X => &A::X,
239223
A::Y => &A::Y,
240224
}
241225
}
226+
227+
fn static_ref(&self) -> Option<&'static Self> {
228+
Some(match self {
229+
A::X => &A::X,
230+
A::Y => &A::Y,
231+
})
232+
}
242233
}
243234

244235
let interner = Interner::default();

crates/bevy_utils/src/label.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ macro_rules! define_label {
160160
fn leak(&self) -> &'static Self {
161161
Box::leak(self.dyn_clone())
162162
}
163-
}
164163

165-
impl ::bevy_utils::intern::StaticRef for dyn $label_trait_name {
166164
fn static_ref(&self) -> std::option::Option<&'static dyn $label_trait_name> {
167165
self.dyn_static_ref()
168166
}

0 commit comments

Comments
 (0)