Skip to content

Commit 83d8854

Browse files
Better type_eq (#2946)
* typeid that doesn't suck * actually, that's not const! * format, move phantomdata to alloc feature block * a --------- Co-authored-by: Dongjia "toka" Zhang <[email protected]>
1 parent abe9551 commit 83d8854

File tree

3 files changed

+5
-40
lines changed

3 files changed

+5
-40
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ strum = "0.26.3"
120120
strum_macros = "0.26.4"
121121
toml = "0.8.19" # For parsing the injections toml file
122122
typed-builder = "0.20.0" # Implement the builder pattern at compiletime
123+
typeid = "1.0.0" # Safe type_eq that doesn't rely on std specialization
123124
uuid = { version = "1.10.0", features = ["serde", "v4"] }
124125
which = "6.0.3"
125126
windows = "0.59.0"

libafl_bolts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ rustversion = { workspace = true }
122122
[dependencies]
123123
libafl_derive = { workspace = true, default-features = true, optional = true }
124124
static_assertions = { workspace = true }
125+
typeid = { workspace = true }
125126

126127
tuple_list = { version = "0.1.3" }
127128
hashbrown = { workspace = true, features = [

libafl_bolts/src/tuples.rs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use alloc::{borrow::Cow, vec::Vec};
66
use core::{
77
any::type_name,
88
fmt::{Debug, Formatter},
9+
marker::PhantomData,
910
ops::{Deref, DerefMut, Index, IndexMut},
1011
};
11-
use core::{any::TypeId, cell::Cell, marker::PhantomData, mem::transmute};
12+
use core::{any::TypeId, mem::transmute};
1213

1314
#[cfg(feature = "alloc")]
1415
use serde::{Deserialize, Serialize};
@@ -21,36 +22,9 @@ use crate::HasLen;
2122
use crate::Named;
2223

2324
/// Returns if the type `T` is equal to `U`, ignoring lifetimes.
24-
#[inline] // this entire call gets optimized away :)
2525
#[must_use]
2626
pub fn type_eq<T: ?Sized, U: ?Sized>() -> bool {
27-
// decider struct: hold a cell (which we will update if the types are unequal) and some
28-
// phantom data using a function pointer to allow for Copy to be implemented
29-
struct W<'a, T: ?Sized, U: ?Sized>(&'a Cell<bool>, PhantomData<fn() -> (&'a T, &'a U)>);
30-
31-
// default implementation: if the types are unequal, we will use the clone implementation
32-
impl<T: ?Sized, U: ?Sized> Clone for W<'_, T, U> {
33-
#[inline]
34-
fn clone(&self) -> Self {
35-
// indicate that the types are unequal
36-
// unfortunately, use of interior mutability (Cell) makes this not const-compatible
37-
// not really possible to get around at this time
38-
self.0.set(false);
39-
W(self.0, self.1)
40-
}
41-
}
42-
43-
// specialized implementation: Copy is only implemented if the types are the same
44-
#[expect(clippy::mismatching_type_param_order)]
45-
impl<T: ?Sized> Copy for W<'_, T, T> {}
46-
47-
let detected = Cell::new(true);
48-
// [].clone() is *specialized* in core.
49-
// Types which implement copy will have their copy implementations used, falling back to clone.
50-
// If the types are the same, then our clone implementation (which sets our Cell to false)
51-
// will never be called, meaning that our Cell's content remains true.
52-
let res = [W::<T, U>(&detected, PhantomData)].clone();
53-
res[0].0.get()
27+
typeid::of::<T>() == typeid::of::<U>()
5428
}
5529

5630
/// Borrow each member of the tuple
@@ -955,8 +929,6 @@ mod test {
955929
}
956930

957931
#[test]
958-
#[cfg(feature = "alloc")]
959-
#[expect(unused_qualifications)] // for type name tests
960932
fn test_type_eq() {
961933
// An alias for equality testing
962934
type OwnedMutSliceAlias<'a> = OwnedMutSlice<'a, u8>;
@@ -976,15 +948,6 @@ mod test {
976948
// test weirder lifetime things
977949
assert!(type_eq::<OwnedMutSlice<u8>, OwnedMutSlice<u8>>());
978950
assert!(!type_eq::<OwnedMutSlice<u8>, OwnedMutSlice<u32>>());
979-
980-
assert!(type_eq::<
981-
OwnedMutSlice<u8>,
982-
crate::ownedref::OwnedMutSlice<u8>,
983-
>());
984-
assert!(!type_eq::<
985-
OwnedMutSlice<u8>,
986-
crate::ownedref::OwnedMutSlice<u32>,
987-
>());
988951
}
989952

990953
#[test]

0 commit comments

Comments
 (0)