Skip to main content

hir_ty/next_solver/
def_id.rs

1//! Definition of `SolverDefId`
2
3use hir_def::{
4    AdtId, AttrDefId, BuiltinDeriveImplId, CallableDefId, ConstId, DefWithBodyId, EnumId,
5    EnumVariantId, ExpressionStoreOwnerId, FunctionId, GenericDefId, ImplId, StaticId, StructId,
6    TraitId, TypeAliasId, UnionId, VariantId,
7    signatures::{
8        ConstSignature, EnumSignature, FunctionSignature, StaticSignature, StructSignature,
9        TraitSignature, TypeAliasSignature, UnionSignature,
10    },
11};
12use rustc_type_ir::inherent;
13use salsa::Update;
14use stdx::impl_from;
15
16use crate::{
17    InferBodyId,
18    db::{
19        AnonConstId, GeneralConstId, InternedClosureId, InternedCoroutineClosureId,
20        InternedCoroutineId, InternedOpaqueTyId,
21    },
22};
23
24use super::DbInterner;
25
26#[derive(Debug, PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash)]
27pub enum Ctor {
28    Struct(StructId),
29    Enum(EnumVariantId),
30}
31
32#[derive(PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash, Update)]
33pub enum SolverDefId<'db> {
34    AdtId(AdtId),
35    ConstId(ConstId),
36    FunctionId(FunctionId),
37    ImplId(ImplId),
38    BuiltinDeriveImplId(BuiltinDeriveImplId),
39    StaticId(StaticId),
40    AnonConstId(AnonConstId<'db>),
41    TraitId(TraitId),
42    TypeAliasId(TypeAliasId),
43    InternedClosureId(InternedClosureId<'db>),
44    InternedCoroutineId(InternedCoroutineId<'db>),
45    InternedCoroutineClosureId(InternedCoroutineClosureId<'db>),
46    InternedOpaqueTyId(InternedOpaqueTyId<'db>),
47    EnumVariantId(EnumVariantId),
48    Ctor(Ctor),
49}
50
51impl std::fmt::Debug for SolverDefId<'_> {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        let interner = DbInterner::conjure();
54        let db = interner.db;
55        match *self {
56            SolverDefId::AdtId(AdtId::StructId(id)) => {
57                f.debug_tuple("AdtId").field(&StructSignature::of(db, id).name.as_str()).finish()
58            }
59            SolverDefId::AdtId(AdtId::EnumId(id)) => {
60                f.debug_tuple("AdtId").field(&EnumSignature::of(db, id).name.as_str()).finish()
61            }
62            SolverDefId::AdtId(AdtId::UnionId(id)) => {
63                f.debug_tuple("AdtId").field(&UnionSignature::of(db, id).name.as_str()).finish()
64            }
65            SolverDefId::ConstId(id) => f
66                .debug_tuple("ConstId")
67                .field(&ConstSignature::of(db, id).name.as_ref().map_or("_", |name| name.as_str()))
68                .finish(),
69            SolverDefId::FunctionId(id) => f
70                .debug_tuple("FunctionId")
71                .field(&FunctionSignature::of(db, id).name.as_str())
72                .finish(),
73            SolverDefId::ImplId(id) => f.debug_tuple("ImplId").field(&id).finish(),
74            SolverDefId::BuiltinDeriveImplId(id) => f.debug_tuple("ImplId").field(&id).finish(),
75            SolverDefId::StaticId(id) => {
76                f.debug_tuple("StaticId").field(&StaticSignature::of(db, id).name.as_str()).finish()
77            }
78            SolverDefId::TraitId(id) => {
79                f.debug_tuple("TraitId").field(&TraitSignature::of(db, id).name.as_str()).finish()
80            }
81            SolverDefId::TypeAliasId(id) => f
82                .debug_tuple("TypeAliasId")
83                .field(&TypeAliasSignature::of(db, id).name.as_str())
84                .finish(),
85            SolverDefId::InternedClosureId(id) => {
86                f.debug_tuple("InternedClosureId").field(&id).finish()
87            }
88            SolverDefId::InternedCoroutineId(id) => {
89                f.debug_tuple("InternedCoroutineId").field(&id).finish()
90            }
91            SolverDefId::InternedCoroutineClosureId(id) => {
92                f.debug_tuple("InternedCoroutineClosureId").field(&id).finish()
93            }
94            SolverDefId::InternedOpaqueTyId(id) => {
95                f.debug_tuple("InternedOpaqueTyId").field(&id).finish()
96            }
97            SolverDefId::EnumVariantId(id) => {
98                let parent_enum = id.loc(db).parent;
99                f.debug_tuple("EnumVariantId")
100                    .field(&format_args!(
101                        "\"{}::{}\"",
102                        EnumSignature::of(db, parent_enum).name.as_str(),
103                        parent_enum.enum_variants(db).variant_name_by_id(id).unwrap().as_str()
104                    ))
105                    .finish()
106            }
107            SolverDefId::AnonConstId(id) => f.debug_tuple("AnonConstId").field(&id).finish(),
108            SolverDefId::Ctor(Ctor::Struct(id)) => {
109                f.debug_tuple("Ctor").field(&StructSignature::of(db, id).name.as_str()).finish()
110            }
111            SolverDefId::Ctor(Ctor::Enum(id)) => {
112                let parent_enum = id.loc(db).parent;
113                f.debug_tuple("Ctor")
114                    .field(&format_args!(
115                        "\"{}::{}\"",
116                        EnumSignature::of(db, parent_enum).name.as_str(),
117                        parent_enum.enum_variants(db).variant_name_by_id(id).unwrap().as_str()
118                    ))
119                    .finish()
120            }
121        }
122    }
123}
124
125impl_from!(
126    impl<'db>
127    AdtId(StructId, EnumId, UnionId),
128    ConstId,
129    FunctionId,
130    ImplId,
131    BuiltinDeriveImplId,
132    StaticId,
133    AnonConstId<'db>,
134    TraitId,
135    TypeAliasId,
136    InternedClosureId<'db>,
137    InternedCoroutineId<'db>,
138    InternedCoroutineClosureId<'db>,
139    InternedOpaqueTyId<'db>,
140    EnumVariantId,
141    Ctor
142    for SolverDefId<'db>
143);
144
145impl_from!(
146    impl<'db>
147    GenericDefId { AdtId, ConstId, FunctionId, ImplId, StaticId, TraitId, TypeAliasId }
148    for SolverDefId<'db>
149);
150
151impl<'db> From<GeneralConstId<'db>> for SolverDefId<'db> {
152    #[inline]
153    fn from(value: GeneralConstId<'db>) -> Self {
154        match value {
155            GeneralConstId::ConstId(const_id) => SolverDefId::ConstId(const_id),
156            GeneralConstId::StaticId(static_id) => SolverDefId::StaticId(static_id),
157            GeneralConstId::AnonConstId(anon_const_id) => SolverDefId::AnonConstId(anon_const_id),
158        }
159    }
160}
161
162impl<'db> From<CallableDefId> for SolverDefId<'db> {
163    #[inline]
164    fn from(value: CallableDefId) -> Self {
165        match value {
166            CallableDefId::FunctionId(id) => id.into(),
167            CallableDefId::StructId(id) => id.into(),
168            CallableDefId::EnumVariantId(id) => id.into(),
169        }
170    }
171}
172
173impl<'db> From<DefWithBodyId> for SolverDefId<'db> {
174    #[inline]
175    fn from(value: DefWithBodyId) -> Self {
176        match value {
177            DefWithBodyId::FunctionId(id) => id.into(),
178            DefWithBodyId::StaticId(id) => id.into(),
179            DefWithBodyId::ConstId(id) => id.into(),
180            DefWithBodyId::VariantId(id) => id.into(),
181        }
182    }
183}
184
185impl<'db> From<InferBodyId<'db>> for SolverDefId<'db> {
186    #[inline]
187    fn from(value: InferBodyId<'db>) -> Self {
188        match value {
189            InferBodyId::DefWithBodyId(id) => id.into(),
190            InferBodyId::AnonConstId(id) => id.into(),
191        }
192    }
193}
194
195impl<'db> From<VariantId> for SolverDefId<'db> {
196    #[inline]
197    fn from(value: VariantId) -> Self {
198        match value {
199            VariantId::EnumVariantId(id) => id.into(),
200            VariantId::StructId(id) => id.into(),
201            VariantId::UnionId(id) => id.into(),
202        }
203    }
204}
205
206impl<'db> From<ExpressionStoreOwnerId> for SolverDefId<'db> {
207    #[inline]
208    fn from(value: ExpressionStoreOwnerId) -> Self {
209        match value {
210            ExpressionStoreOwnerId::Body(body_id) => body_id.into(),
211            ExpressionStoreOwnerId::Signature(sig_id) => sig_id.into(),
212            ExpressionStoreOwnerId::VariantFields(variant_id) => variant_id.into(),
213        }
214    }
215}
216
217impl TryFrom<SolverDefId<'_>> for AttrDefId {
218    type Error = ();
219    #[inline]
220    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
221        match value {
222            SolverDefId::AdtId(it) => Ok(it.into()),
223            SolverDefId::ConstId(it) => Ok(it.into()),
224            SolverDefId::FunctionId(it) => Ok(it.into()),
225            SolverDefId::ImplId(it) => Ok(it.into()),
226            SolverDefId::StaticId(it) => Ok(it.into()),
227            SolverDefId::TraitId(it) => Ok(it.into()),
228            SolverDefId::TypeAliasId(it) => Ok(it.into()),
229            SolverDefId::EnumVariantId(it) => Ok(it.into()),
230            SolverDefId::Ctor(Ctor::Struct(it)) => Ok(it.into()),
231            SolverDefId::Ctor(Ctor::Enum(it)) => Ok(it.into()),
232            SolverDefId::BuiltinDeriveImplId(_)
233            | SolverDefId::InternedClosureId(_)
234            | SolverDefId::InternedCoroutineId(_)
235            | SolverDefId::InternedCoroutineClosureId(_)
236            | SolverDefId::InternedOpaqueTyId(_)
237            | SolverDefId::AnonConstId(_) => Err(()),
238        }
239    }
240}
241
242impl TryFrom<SolverDefId<'_>> for DefWithBodyId {
243    type Error = ();
244
245    #[inline]
246    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
247        let id = match value {
248            SolverDefId::ConstId(id) => id.into(),
249            SolverDefId::FunctionId(id) => id.into(),
250            SolverDefId::StaticId(id) => id.into(),
251            SolverDefId::EnumVariantId(id) | SolverDefId::Ctor(Ctor::Enum(id)) => id.into(),
252            SolverDefId::InternedOpaqueTyId(_)
253            | SolverDefId::TraitId(_)
254            | SolverDefId::TypeAliasId(_)
255            | SolverDefId::ImplId(_)
256            | SolverDefId::BuiltinDeriveImplId(_)
257            | SolverDefId::InternedClosureId(_)
258            | SolverDefId::InternedCoroutineId(_)
259            | SolverDefId::InternedCoroutineClosureId(_)
260            | SolverDefId::Ctor(Ctor::Struct(_))
261            | SolverDefId::AnonConstId(_)
262            | SolverDefId::AdtId(_) => return Err(()),
263        };
264        Ok(id)
265    }
266}
267
268impl<'db> TryFrom<SolverDefId<'db>> for InferBodyId<'db> {
269    type Error = ();
270
271    #[inline]
272    fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> {
273        let id = match value {
274            SolverDefId::ConstId(id) => id.into(),
275            SolverDefId::FunctionId(id) => id.into(),
276            SolverDefId::StaticId(id) => id.into(),
277            SolverDefId::EnumVariantId(id) | SolverDefId::Ctor(Ctor::Enum(id)) => id.into(),
278            SolverDefId::AnonConstId(id) => id.into(),
279            SolverDefId::InternedOpaqueTyId(_)
280            | SolverDefId::TraitId(_)
281            | SolverDefId::TypeAliasId(_)
282            | SolverDefId::ImplId(_)
283            | SolverDefId::BuiltinDeriveImplId(_)
284            | SolverDefId::InternedClosureId(_)
285            | SolverDefId::InternedCoroutineId(_)
286            | SolverDefId::InternedCoroutineClosureId(_)
287            | SolverDefId::Ctor(Ctor::Struct(_))
288            | SolverDefId::AdtId(_) => return Err(()),
289        };
290        Ok(id)
291    }
292}
293
294impl TryFrom<SolverDefId<'_>> for GenericDefId {
295    type Error = ();
296
297    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
298        Ok(match value {
299            SolverDefId::AdtId(adt_id) => GenericDefId::AdtId(adt_id),
300            SolverDefId::ConstId(const_id) => GenericDefId::ConstId(const_id),
301            SolverDefId::FunctionId(function_id) => GenericDefId::FunctionId(function_id),
302            SolverDefId::ImplId(impl_id) => GenericDefId::ImplId(impl_id),
303            SolverDefId::StaticId(static_id) => GenericDefId::StaticId(static_id),
304            SolverDefId::TraitId(trait_id) => GenericDefId::TraitId(trait_id),
305            SolverDefId::TypeAliasId(type_alias_id) => GenericDefId::TypeAliasId(type_alias_id),
306            SolverDefId::InternedClosureId(_)
307            | SolverDefId::InternedCoroutineId(_)
308            | SolverDefId::InternedCoroutineClosureId(_)
309            | SolverDefId::InternedOpaqueTyId(_)
310            | SolverDefId::EnumVariantId(_)
311            | SolverDefId::BuiltinDeriveImplId(_)
312            | SolverDefId::AnonConstId(_)
313            | SolverDefId::Ctor(_) => return Err(()),
314        })
315    }
316}
317
318impl<'db> inherent::DefId<DbInterner<'db>> for SolverDefId<'db> {
319    fn as_local(self) -> Option<SolverDefId<'db>> {
320        Some(self)
321    }
322    fn is_local(self) -> bool {
323        true
324    }
325}
326
327macro_rules! declare_id_wrapper {
328    ($name:ident, $wraps:ident) => {
329        declare_id_wrapper!($name, $wraps, SolverDefId<'db>);
330    };
331
332    ($name:ident, $wraps:ident, $local:ty) => {
333        declare_id_wrapper!($name, $wraps, $local, no_try_from);
334
335        impl TryFrom<SolverDefId<'_>> for $name {
336            type Error = ();
337
338            #[inline]
339            fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
340                match value {
341                    SolverDefId::$wraps(it) => Ok(Self(it)),
342                    _ => Err(()),
343                }
344            }
345        }
346    };
347
348    ($name:ident, $wraps:ident, $local:ty, no_try_from) => {
349        #[derive(Clone, Copy, PartialEq, Eq, Hash)]
350        pub struct $name(pub $wraps);
351
352        impl std::fmt::Debug for $name {
353            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
354                std::fmt::Debug::fmt(&SolverDefId::from(self.0), f)
355            }
356        }
357
358        impl From<$name> for $wraps {
359            #[inline]
360            fn from(value: $name) -> $wraps {
361                value.0
362            }
363        }
364
365        impl From<$wraps> for $name {
366            #[inline]
367            fn from(value: $wraps) -> $name {
368                Self(value)
369            }
370        }
371
372        impl<'db> From<$name> for SolverDefId<'db> {
373            #[inline]
374            fn from(value: $name) -> SolverDefId<'db> {
375                value.0.into()
376            }
377        }
378
379        impl<'db> inherent::DefId<DbInterner<'db>, $local> for $name {
380            fn as_local(self) -> Option<$local> {
381                Some(self.into())
382            }
383            fn is_local(self) -> bool {
384                true
385            }
386        }
387    };
388}
389
390/// This is similar to [`declare_id_wrapper`], but handles ids which have the `'db` lifetime.
391macro_rules! declare_id_wrapper_with_lt {
392    ($name:ident, $wraps:ident) => {
393        declare_id_wrapper_with_lt!($name, $wraps, SolverDefId<'db>);
394    };
395
396    ($name:ident, $wraps:ident, $local:ty) => {
397        declare_id_wrapper_with_lt!($name, $wraps, $local, no_try_from);
398
399        impl<'db> TryFrom<SolverDefId<'db>> for $name<'db> {
400            type Error = ();
401
402            #[inline]
403            fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> {
404                match value {
405                    SolverDefId::$wraps(it) => Ok(Self(it)),
406                    _ => Err(()),
407                }
408            }
409        }
410    };
411
412    ($name:ident, $wraps:ident, $local:ty, no_try_from) => {
413        #[derive(Clone, Copy, PartialEq, Eq, Hash)]
414        pub struct $name<'db>(pub $wraps<'db>);
415
416        impl std::fmt::Debug for $name<'_> {
417            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
418                std::fmt::Debug::fmt(&SolverDefId::from(self.0), f)
419            }
420        }
421
422        impl<'db> From<$name<'db>> for $wraps<'db> {
423            #[inline]
424            fn from(value: $name<'db>) -> $wraps<'db> {
425                value.0
426            }
427        }
428
429        impl<'db> From<$wraps<'db>> for $name<'db> {
430            #[inline]
431            fn from(value: $wraps<'db>) -> $name<'db> {
432                Self(value)
433            }
434        }
435
436        impl<'db> From<$name<'db>> for SolverDefId<'db> {
437            #[inline]
438            fn from(value: $name<'db>) -> SolverDefId<'db> {
439                value.0.into()
440            }
441        }
442
443        impl<'db> inherent::DefId<DbInterner<'db>, $local> for $name<'db> {
444            fn as_local(self) -> Option<$local> {
445                Some(self.into())
446            }
447            fn is_local(self) -> bool {
448                true
449            }
450        }
451    };
452}
453
454declare_id_wrapper!(TraitIdWrapper, TraitId);
455declare_id_wrapper!(TypeAliasIdWrapper, TypeAliasId);
456declare_id_wrapper_with_lt!(ClosureIdWrapper, InternedClosureId);
457declare_id_wrapper_with_lt!(CoroutineIdWrapper, InternedCoroutineId);
458declare_id_wrapper_with_lt!(CoroutineClosureIdWrapper, InternedCoroutineClosureId);
459declare_id_wrapper!(AdtIdWrapper, AdtId);
460declare_id_wrapper_with_lt!(OpaqueTyIdWrapper, InternedOpaqueTyId, OpaqueTyIdWrapper<'db>);
461
462macro_rules! declare_ty_const_pair {
463    ( $ty_id_name:ident, $const_id_name:ident, $term_id_name:ident ) => {
464        declare_id_wrapper!($ty_id_name, TypeAliasId);
465        declare_id_wrapper!($const_id_name, ConstId);
466        declare_id_wrapper!($term_id_name, TermId, SolverDefId<'db>, no_try_from);
467
468        impl TryFrom<SolverDefId<'_>> for $term_id_name {
469            type Error = ();
470
471            #[inline]
472            fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
473                match value {
474                    SolverDefId::TypeAliasId(it) => Ok(Self(TermId::TypeAliasId(it))),
475                    SolverDefId::ConstId(it) => Ok(Self(TermId::ConstId(it))),
476                    _ => Err(()),
477                }
478            }
479        }
480
481        impl From<$ty_id_name> for $term_id_name {
482            fn from(value: $ty_id_name) -> Self {
483                $term_id_name(TermId::TypeAliasId(value.0))
484            }
485        }
486
487        impl From<$const_id_name> for $term_id_name {
488            fn from(value: $const_id_name) -> Self {
489                $term_id_name(TermId::ConstId(value.0))
490            }
491        }
492
493        impl TryFrom<$term_id_name> for $ty_id_name {
494            type Error = ();
495
496            fn try_from(value: $term_id_name) -> Result<Self, Self::Error> {
497                match value.0 {
498                    TermId::TypeAliasId(id) => Ok($ty_id_name(id)),
499                    TermId::ConstId(_) => Err(()),
500                }
501            }
502        }
503
504        impl TryFrom<$term_id_name> for $const_id_name {
505            type Error = ();
506
507            fn try_from(value: $term_id_name) -> Result<Self, Self::Error> {
508                match value.0 {
509                    TermId::ConstId(id) => Ok($const_id_name(id)),
510                    TermId::TypeAliasId(_) => Err(()),
511                }
512            }
513        }
514
515        impl<'db> From<$const_id_name> for GeneralConstIdWrapper<'db> {
516            fn from(value: $const_id_name) -> Self {
517                GeneralConstIdWrapper(GeneralConstId::ConstId(value.0))
518            }
519        }
520    };
521}
522
523declare_ty_const_pair!(TraitAssocTyId, TraitAssocConstId, TraitAssocTermId);
524declare_ty_const_pair!(ImplOrTraitAssocTyId, ImplOrTraitAssocConstId, ImplOrTraitAssocTermId);
525declare_ty_const_pair!(FreeTyAliasId, FreeConstAliasId, FreeTermAliasId);
526declare_ty_const_pair!(InherentAssocTyId, InherentAssocConstId, InherentAssocTermId);
527
528#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
529pub enum TermId {
530    TypeAliasId(TypeAliasId),
531    ConstId(ConstId),
532}
533impl_from!(TypeAliasId, ConstId for TermId);
534
535impl<'db> From<TermId> for SolverDefId<'db> {
536    fn from(value: TermId) -> Self {
537        match value {
538            TermId::TypeAliasId(id) => id.into(),
539            TermId::ConstId(id) => id.into(),
540        }
541    }
542}
543
544#[derive(Clone, Copy, PartialEq, Eq, Hash)]
545pub struct GeneralConstIdWrapper<'db>(pub GeneralConstId<'db>);
546
547impl std::fmt::Debug for GeneralConstIdWrapper<'_> {
548    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
549        std::fmt::Debug::fmt(&self.0, f)
550    }
551}
552impl<'db> From<GeneralConstIdWrapper<'db>> for GeneralConstId<'db> {
553    #[inline]
554    fn from(value: GeneralConstIdWrapper<'db>) -> GeneralConstId<'db> {
555        value.0
556    }
557}
558impl<'db> From<GeneralConstId<'db>> for GeneralConstIdWrapper<'db> {
559    #[inline]
560    fn from(value: GeneralConstId<'db>) -> GeneralConstIdWrapper<'db> {
561        Self(value)
562    }
563}
564impl<'db> From<GeneralConstIdWrapper<'db>> for SolverDefId<'db> {
565    #[inline]
566    fn from(value: GeneralConstIdWrapper<'db>) -> SolverDefId<'db> {
567        match value.0 {
568            GeneralConstId::ConstId(id) => SolverDefId::ConstId(id),
569            GeneralConstId::StaticId(id) => SolverDefId::StaticId(id),
570            GeneralConstId::AnonConstId(id) => SolverDefId::AnonConstId(id),
571        }
572    }
573}
574impl<'db> TryFrom<SolverDefId<'db>> for GeneralConstIdWrapper<'db> {
575    type Error = ();
576    #[inline]
577    fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> {
578        match value {
579            SolverDefId::ConstId(it) => Ok(Self(it.into())),
580            SolverDefId::StaticId(it) => Ok(Self(it.into())),
581            SolverDefId::AnonConstId(it) => Ok(Self(it.into())),
582            _ => Err(()),
583        }
584    }
585}
586impl<'db> inherent::DefId<DbInterner<'db>> for GeneralConstIdWrapper<'db> {
587    fn as_local(self) -> Option<SolverDefId<'db>> {
588        Some(self.into())
589    }
590    fn is_local(self) -> bool {
591        true
592    }
593}
594
595#[derive(Clone, Copy, PartialEq, Eq, Hash)]
596pub struct CallableIdWrapper(pub CallableDefId);
597
598impl std::fmt::Debug for CallableIdWrapper {
599    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
600        std::fmt::Debug::fmt(&self.0, f)
601    }
602}
603impl From<CallableIdWrapper> for CallableDefId {
604    #[inline]
605    fn from(value: CallableIdWrapper) -> CallableDefId {
606        value.0
607    }
608}
609impl From<CallableDefId> for CallableIdWrapper {
610    #[inline]
611    fn from(value: CallableDefId) -> CallableIdWrapper {
612        Self(value)
613    }
614}
615impl<'db> From<CallableIdWrapper> for SolverDefId<'db> {
616    #[inline]
617    fn from(value: CallableIdWrapper) -> SolverDefId<'db> {
618        match value.0 {
619            CallableDefId::FunctionId(it) => it.into(),
620            CallableDefId::StructId(it) => Ctor::Struct(it).into(),
621            CallableDefId::EnumVariantId(it) => Ctor::Enum(it).into(),
622        }
623    }
624}
625impl TryFrom<SolverDefId<'_>> for CallableIdWrapper {
626    type Error = ();
627    #[inline]
628    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
629        match value {
630            SolverDefId::FunctionId(it) => Ok(Self(it.into())),
631            SolverDefId::Ctor(Ctor::Struct(it)) => Ok(Self(it.into())),
632            SolverDefId::Ctor(Ctor::Enum(it)) => Ok(Self(it.into())),
633            _ => Err(()),
634        }
635    }
636}
637impl<'db> inherent::DefId<DbInterner<'db>> for CallableIdWrapper {
638    fn as_local(self) -> Option<SolverDefId<'db>> {
639        Some(self.into())
640    }
641    fn is_local(self) -> bool {
642        true
643    }
644}
645
646#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
647pub enum AnyImplId {
648    ImplId(ImplId),
649    BuiltinDeriveImplId(BuiltinDeriveImplId),
650}
651
652impl_from!(ImplId, BuiltinDeriveImplId for AnyImplId);
653
654impl<'db> From<AnyImplId> for SolverDefId<'db> {
655    #[inline]
656    fn from(value: AnyImplId) -> SolverDefId<'db> {
657        match value {
658            AnyImplId::ImplId(it) => it.into(),
659            AnyImplId::BuiltinDeriveImplId(it) => it.into(),
660        }
661    }
662}
663impl TryFrom<SolverDefId<'_>> for AnyImplId {
664    type Error = ();
665    #[inline]
666    fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> {
667        match value {
668            SolverDefId::ImplId(it) => Ok(it.into()),
669            SolverDefId::BuiltinDeriveImplId(it) => Ok(it.into()),
670            _ => Err(()),
671        }
672    }
673}
674impl<'db> inherent::DefId<DbInterner<'db>> for AnyImplId {
675    fn as_local(self) -> Option<SolverDefId<'db>> {
676        Some(self.into())
677    }
678    fn is_local(self) -> bool {
679        true
680    }
681}