Skip to content

Commit

Permalink
hide vtable implementation details
Browse files Browse the repository at this point in the history
  • Loading branch information
JoJoJet committed Aug 12, 2022
1 parent 0231141 commit 989b916
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions crates/bevy_utils/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,20 @@ macro_rules! define_label {

$(#[$label_attr])*
pub trait $label_name: 'static {
/// Essentially acts like a dynamic dispatch virtual table,
/// but specialized for labels.
const VTABLE : $crate::label::VTable = $crate::label::VTable {
ty: || ::std::any::TypeId::of::<Self>(),
fmt: Self::fmt,
};
/// Converts this type into an opaque, strongly-typed label.
#[inline]
fn as_label(&self) -> $id_name {
// This is just machinery that lets us store the TypeId and formatter fn in the same static reference.
struct VTables<L: ?::std::marker::Sized>(::std::marker::PhantomData<L>);
impl<L: $label_name + ?::std::marker::Sized> VTables<L> {
const VTABLE: $crate::label::VTable = $crate::label::VTable {
ty: || ::std::any::TypeId::of::<L>(),
fmt: <L as $label_name>::fmt,
};
}

let data = self.data();
$id_name { data, vtable: &Self::VTABLE }
$id_name { data, vtable: &VTables::<Self>::VTABLE }
}
/// Returns a number used to distinguish different labels of the same type.
fn data(&self) -> u64;
Expand Down

0 comments on commit 989b916

Please sign in to comment.