abi_stable/std_types/map/
map_query.rs1use super::*;
2
3#[derive(StableAbi)]
5#[repr(C)]
6pub struct MapQuery<'a, K> {
7 _marker: NotCopyNotClone,
8 is_equal: extern "C" fn(&K, RRef<'_, ErasedObject>) -> bool,
9 hash: extern "C" fn(RRef<'_, ErasedObject>, HasherObject<'_>),
10 query: RRef<'a, ErasedObject>,
11}
12
13impl<'a, K> MapQuery<'a, K> {
14 #[inline]
15 pub(super) fn new<Q>(query: &'a &'a Q) -> Self
16 where
17 K: Borrow<Q>,
18 Q: Hash + Eq + 'a + ?Sized,
19 {
20 MapQuery {
21 _marker: NotCopyNotClone,
22 is_equal: is_equal::<K, Q>,
23 hash: hash::<Q>,
24 query: unsafe { RRef::new(query).transmute() },
25 }
26 }
27
28 #[inline]
29 pub(super) unsafe fn as_static(&self) -> &MapQuery<'static, K> {
30 unsafe { crate::utils::transmute_reference(self) }
31 }
32}
33
34impl<'a, K> MapQuery<'a, K> {
35 #[inline]
36 pub(super) fn is_equal(&self, other: &K) -> bool {
37 (self.is_equal)(other, self.query)
38 }
39
40 #[inline]
41 pub(super) unsafe fn as_mapkey(&self) -> MapKey<K> {
42 MapKey::Query(NonNull::from(unsafe { self.as_static() }))
43 }
44}
45
46impl<'a, K> Hash for MapQuery<'a, K> {
47 #[inline]
48 fn hash<H>(&self, hasher: &mut H)
49 where
50 H: Hasher,
51 {
52 (self.hash)(self.query, HasherObject::new(hasher))
53 }
54}
55
56extern "C" fn is_equal<K, Q>(key: &K, query: RRef<'_, ErasedObject>) -> bool
57where
58 K: Borrow<Q>,
59 Q: Eq + ?Sized,
60{
61 extern_fn_panic_handling! {
62 let query = unsafe{ query.transmute_into_ref::<&Q>() };
63 key.borrow() == *query
64 }
65}
66
67extern "C" fn hash<Q>(query: RRef<'_, ErasedObject>, mut hasher: HasherObject<'_>)
68where
69 Q: Hash + ?Sized,
70{
71 extern_fn_panic_handling! {
72 let query = unsafe{ query.transmute_into_ref::<&Q>() };
73 query.hash(&mut hasher);
74 }
75}