Skip to content

Commit

Permalink
simplify intener APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
JoJoJet committed Aug 7, 2022
1 parent 430b5a6 commit 4fa714d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
25 changes: 4 additions & 21 deletions crates/bevy_ecs/src/schedule/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ macro_rules! impl_string_label {
$crate::schedule::STR_INTERN.intern(self)
}
fn fmt(idx: u64, f: &mut std::fmt::Formatter) -> std::fmt::Result {
$crate::schedule::STR_INTERN
.scope(idx, |s: &Self| write!(f, "{s}"))
.ok_or(::std::fmt::Error)?
let s = $crate::schedule::STR_INTERN
.get(idx)
.ok_or(std::fmt::Error)?;
write!(f, "{s}")
}
}
};
Expand Down Expand Up @@ -97,14 +98,6 @@ impl<T: Clone + Hash + Eq> TypedLabels<T> {
idx as u64
}

/// Allows one to peek at an interned label and execute code,
/// optionally returning a value.
///
/// Returns `None` if there is no interned label with that key.
pub fn scope<U>(&self, idx: u64, f: impl FnOnce(&T) -> U) -> Option<U> {
self.0.read().get_index(idx as usize).map(f)
}

/// Gets a reference to the label with specified index.
pub fn get(&self, idx: u64) -> Option<LabelGuard<T>> {
RwLockReadGuard::try_map(self.0.read(), |set| set.get_index(idx as usize)).ok()
Expand Down Expand Up @@ -193,16 +186,6 @@ impl Labels {
}
}

/// Allows one to peek at an interned label and execute code,
/// optionally returning a value.
///
/// Returns `None` if there is no interned label with that key.
pub fn scope<L: 'static, U>(&self, key: u64, f: impl FnOnce(&L) -> U) -> Option<U> {
let type_map = self.0.read();
let set = type_map.get::<IndexSet<L>>()?;
set.get_index(key as usize).map(f)
}

/// Gets a reference to the label with specified index.
pub fn get<L: 'static>(&self, key: u64) -> Option<LabelGuard<L>> {
RwLockReadGuard::try_map(self.0.read(), |type_map| {
Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_macro_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,8 @@ fn derive_interned_label(
#interner_ident .intern(self)
}
fn fmt(idx: u64, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
#interner_ident
.scope(idx, |val: &Self| ::std::fmt::Debug::fmt(val, f))
.ok_or(::std::fmt::Error)?
let val: #guard_type_path <Self> = #interner_ident .get(idx).ok_or(::std::fmt::Error)?;
::std::fmt::Debug::fmt(&*val, f)
}
}

Expand Down

0 comments on commit 4fa714d

Please sign in to comment.