Skip to content

Represent type names with generic lifetime parameters #622

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 2 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
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
34 changes: 33 additions & 1 deletion syntax/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::syntax::{
Array, ExternFn, Impl, Include, Receiver, Ref, Signature, SliceRef, Ty1, Type, Var,
Array, ExternFn, Impl, Include, Lifetimes, Receiver, Ref, Signature, SliceRef, Ty1, Type, Var,
};
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -81,6 +81,38 @@ impl PartialEq for Type {
}
}

impl Eq for Lifetimes {}

impl PartialEq for Lifetimes {
fn eq(&self, other: &Lifetimes) -> bool {
let Lifetimes {
lt_token: _,
lifetimes,
gt_token: _,
} = self;
let Lifetimes {
lt_token: _,
lifetimes: lifetimes2,
gt_token: _,
} = other;
lifetimes.iter().eq(lifetimes2)
}
}

impl Hash for Lifetimes {
fn hash<H: Hasher>(&self, state: &mut H) {
let Lifetimes {
lt_token: _,
lifetimes,
gt_token: _,
} = self;
lifetimes.len().hash(state);
for lifetime in lifetimes {
lifetime.hash(state);
}
}
}

impl Eq for Ty1 {}

impl PartialEq for Ty1 {
Expand Down
4 changes: 2 additions & 2 deletions syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ pub struct Pair {

// Wrapper for a type which needs to be resolved before it can be printed in
// C++.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
#[derive(PartialEq, Eq, Hash)]
pub struct RustName {
pub rust: Ident,
pub generics: Lifetimes,
}
14 changes: 8 additions & 6 deletions syntax/names.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::syntax::{Pair, RustName, Symbol, Types};
use crate::syntax::{Lifetimes, Pair, RustName, Symbol, Types};
use proc_macro2::{Ident, Span};
use std::iter;
use syn::punctuated::Punctuated;

impl Pair {
pub fn to_symbol(&self) -> Symbol {
Expand All @@ -23,11 +24,12 @@ impl Pair {

impl RustName {
pub fn new(rust: Ident) -> Self {
RustName { rust }
}

pub fn from_ref(rust: &Ident) -> &Self {
unsafe { &*(rust as *const Ident as *const Self) }
let generics = Lifetimes {
lt_token: None,
lifetimes: Punctuated::new(),
gt_token: None,
};
RustName { rust, generics }
}

pub fn span(&self) -> Span {
Expand Down
2 changes: 1 addition & 1 deletion syntax/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl ToTokens for Signature {

impl ToTokens for RustName {
fn to_tokens(&self, tokens: &mut TokenStream) {
let RustName { rust } = self;
let RustName { rust, generics: _ } = self;
rust.to_tokens(tokens);
}
}
Expand Down
8 changes: 5 additions & 3 deletions syntax/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Types<'a> {
pub untrusted: Map<&'a Ident, &'a ExternType>,
pub required_trivial: Map<&'a Ident, Vec<TrivialReason<'a>>>,
pub explicit_impls: Set<&'a Impl>,
pub resolutions: Map<&'a RustName, &'a Pair>,
pub resolutions: Map<&'a Ident, &'a Pair>,
pub struct_improper_ctypes: UnorderedSet<&'a Ident>,
pub toposorted_structs: Vec<&'a Struct>,
}
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<'a> Types<'a> {
}

let mut add_resolution = |pair: &'a Pair| {
resolutions.insert(RustName::from_ref(&pair.rust), pair);
resolutions.insert(&pair.rust, pair);
};

let mut type_names = UnorderedSet::new();
Expand Down Expand Up @@ -238,7 +238,9 @@ impl<'a> Types<'a> {
}

pub fn resolve(&self, ident: &RustName) -> &Pair {
self.resolutions.get(ident).expect("Unable to resolve type")
self.resolutions
.get(&ident.rust)
.expect("Unable to resolve type")
}
}

Expand Down