Skip to main content

miden_project/dependencies/resolver/
package_id.rs

1use alloc::{string::ToString, sync::Arc};
2use core::{borrow::Borrow, fmt, ops::Deref};
3
4/// A type that represents the unique identifier for packages in a [`super::PackageIndex`].
5///
6/// This is a simple newtype wrapper around an [`Arc<str>`] so that we can provide some ergonomic
7/// conveniences, and allow migration to some other type in the future with minimal downstream
8/// impact, if any.
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[repr(transparent)]
11pub struct PackageId(Arc<str>);
12
13impl Borrow<str> for PackageId {
14    fn borrow(&self) -> &str {
15        &self.0
16    }
17}
18
19impl Borrow<Arc<str>> for PackageId {
20    fn borrow(&self) -> &Arc<str> {
21        &self.0
22    }
23}
24
25impl fmt::Display for PackageId {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        fmt::Display::fmt(&self.0, f)
28    }
29}
30
31impl AsRef<str> for PackageId {
32    #[inline(always)]
33    fn as_ref(&self) -> &str {
34        self.0.as_ref()
35    }
36}
37
38impl AsRef<Arc<str>> for PackageId {
39    #[inline(always)]
40    fn as_ref(&self) -> &Arc<str> {
41        &self.0
42    }
43}
44
45impl Deref for PackageId {
46    type Target = str;
47
48    #[inline(always)]
49    fn deref(&self) -> &Self::Target {
50        self.0.as_ref()
51    }
52}
53
54impl From<Arc<str>> for PackageId {
55    fn from(value: Arc<str>) -> Self {
56        Self(value)
57    }
58}
59
60impl From<&str> for PackageId {
61    fn from(value: &str) -> Self {
62        Self(value.to_string().into_boxed_str().into())
63    }
64}