Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ pub enum TypeBindingKind {
Constraint(Vec<GenericBound>),
}

#[doc(hidden)]
#[repr(transparent)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct IdType<T: ?Sized>(T);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this? I think It'd be better to just duplicate all the derives, rather than using a type alias here.


/// An opaque identifier for an item.
///
/// It can be used to lookup in [`Crate::index`] or [`Crate::paths`] to resolve it
Expand All @@ -296,11 +301,28 @@ pub enum TypeBindingKind {
/// Rustdoc makes no guarantees about the inner value of Id's. Applications
/// should treat them as opaque keys to lookup items, and avoid attempting
/// to parse them, or otherwise depend on any implementation details.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
// FIXME(aDotInTheVoid): Consider making this non-public in rustdoc-types.
pub struct Id(pub String);
pub type Id = IdType<Box<str>>;

/// A reference to an [`Id`]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But an IdRef isn't a reference to an Id, but an usized typed than can be taken as a reference to. &IdRef is a reference to an Id.

Also, this doc comment should explain the motivations for doing this.

pub type IdRef = IdType<str>;

impl std::ops::Deref for Id {
type Target = IdRef;

fn deref(&self) -> &Self::Target {
let inner: &str = self.0.as_ref();
// Safety: &IdRef has the same layout and lifetime as &str due to the repr(transparent)
unsafe { std::mem::transmute(inner) }
}
}

impl std::borrow::Borrow<IdRef> for Id {
fn borrow(&self) -> &IdRef {
self
}
}

/// The fundamental kind of an item. Unlike [`ItemEnum`], this does not carry any aditional info.
/// The fundamental kind of an item. Unlike [`ItemEnum`], this does not carry any additional info.
///
/// Part of [`ItemSummary`].
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down
Loading