Skip to content

Commit 8864321

Browse files
committed
hide vtable implementation details
1 parent 55be0a0 commit 8864321

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

crates/bevy_utils/src/label.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,20 @@ macro_rules! define_label {
101101

102102
$(#[$label_attr])*
103103
pub trait $label_name: 'static {
104-
/// Essentially acts like a trait object virtual table,
105-
/// but specialized for labels.
106-
const VTABLE : $crate::label::VTable = $crate::label::VTable {
107-
ty: || ::std::any::TypeId::of::<Self>(),
108-
fmt: Self::fmt,
109-
};
110104
/// Converts this type into an opaque, strongly-typed label.
111105
#[inline]
112106
fn as_label(&self) -> $id_name {
107+
// This is just machinery that lets us store the TypeId and formatter fn in the same static reference.
108+
struct VTables<L: ?::std::marker::Sized>(::std::marker::PhantomData<L>);
109+
impl<L: $label_name + ?::std::marker::Sized> VTables<L> {
110+
const VTABLE: $crate::label::VTable = $crate::label::VTable {
111+
ty: || ::std::any::TypeId::of::<Self>(),
112+
fmt: <L as $label_name>::fmt,
113+
};
114+
}
115+
113116
let data = self.data();
114-
$id_name { data, vtable: &Self::VTABLE }
117+
$id_name { data, vtable: &VTables::<Self>::VTABLE }
115118
}
116119
/// Returns a number used to distinguish different labels of the same type.
117120
fn data(&self) -> u64;

0 commit comments

Comments
 (0)