-
-
Notifications
You must be signed in to change notification settings - Fork 388
Better type_eq #2946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Better type_eq #2946
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,10 @@ use alloc::{borrow::Cow, vec::Vec}; | |
use core::{ | ||
any::type_name, | ||
fmt::{Debug, Formatter}, | ||
marker::PhantomData, | ||
ops::{Deref, DerefMut, Index, IndexMut}, | ||
}; | ||
use core::{any::TypeId, cell::Cell, marker::PhantomData, mem::transmute}; | ||
use core::{any::TypeId, mem::transmute}; | ||
|
||
#[cfg(feature = "alloc")] | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -21,36 +22,9 @@ use crate::HasLen; | |
use crate::Named; | ||
|
||
/// Returns if the type `T` is equal to `U`, ignoring lifetimes. | ||
#[inline] // this entire call gets optimized away :) | ||
#[must_use] | ||
pub fn type_eq<T: ?Sized, U: ?Sized>() -> bool { | ||
// decider struct: hold a cell (which we will update if the types are unequal) and some | ||
// phantom data using a function pointer to allow for Copy to be implemented | ||
struct W<'a, T: ?Sized, U: ?Sized>(&'a Cell<bool>, PhantomData<fn() -> (&'a T, &'a U)>); | ||
|
||
// default implementation: if the types are unequal, we will use the clone implementation | ||
impl<T: ?Sized, U: ?Sized> Clone for W<'_, T, U> { | ||
#[inline] | ||
fn clone(&self) -> Self { | ||
// indicate that the types are unequal | ||
// unfortunately, use of interior mutability (Cell) makes this not const-compatible | ||
// not really possible to get around at this time | ||
self.0.set(false); | ||
W(self.0, self.1) | ||
} | ||
} | ||
|
||
// specialized implementation: Copy is only implemented if the types are the same | ||
#[expect(clippy::mismatching_type_param_order)] | ||
impl<T: ?Sized> Copy for W<'_, T, T> {} | ||
|
||
let detected = Cell::new(true); | ||
// [].clone() is *specialized* in core. | ||
// Types which implement copy will have their copy implementations used, falling back to clone. | ||
// If the types are the same, then our clone implementation (which sets our Cell to false) | ||
// will never be called, meaning that our Cell's content remains true. | ||
let res = [W::<T, U>(&detected, PhantomData)].clone(); | ||
res[0].0.get() | ||
typeid::of::<T>() == typeid::of::<U>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use this typeid for our SerdeAnyMap well? |
||
} | ||
|
||
/// Borrow each member of the tuple | ||
|
@@ -955,8 +929,6 @@ mod test { | |
} | ||
|
||
#[test] | ||
#[cfg(feature = "alloc")] | ||
#[expect(unused_qualifications)] // for type name tests | ||
fn test_type_eq() { | ||
// An alias for equality testing | ||
type OwnedMutSliceAlias<'a> = OwnedMutSlice<'a, u8>; | ||
|
@@ -976,15 +948,6 @@ mod test { | |
// test weirder lifetime things | ||
assert!(type_eq::<OwnedMutSlice<u8>, OwnedMutSlice<u8>>()); | ||
assert!(!type_eq::<OwnedMutSlice<u8>, OwnedMutSlice<u32>>()); | ||
|
||
assert!(type_eq::< | ||
OwnedMutSlice<u8>, | ||
crate::ownedref::OwnedMutSlice<u8>, | ||
>()); | ||
assert!(!type_eq::< | ||
OwnedMutSlice<u8>, | ||
crate::ownedref::OwnedMutSlice<u32>, | ||
>()); | ||
} | ||
|
||
#[test] | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please fully specify the version ("1.0.0")?
we moved a few months ago to this convention for other dependencies