Skip to content
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

The ability to generate trait methods using types #1054

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/bindgen/ir/generic_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ impl GenericPath {
// Caller deals with generics.
}

pub fn set_assoc_ty<T>(&mut self, assoc_ty_name: T)
where
T: Into<String>,
{
let path = assoc_ty_name.into();
self.path = Path::new(&path);
self.export_name = path;
}

pub fn path(&self) -> &Path {
&self.path
}
Expand Down
32 changes: 32 additions & 0 deletions src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,38 @@ impl Type {
}
}

pub fn try_set_assoc_name<T>(&mut self, assoc_name: T) -> Result<(), String>
where
T: Into<String>,
{
let mut current = self;
loop {
match current {
Type::Ptr { ref mut ty, .. } => current = ty,
Type::Path(ref mut generic) => {
generic.set_assoc_ty(assoc_name);

break Ok(());
}
_ => {
break Err(format!(
"Failed to set path ({}) for type {:?}",
assoc_name.into(),
current
))
}
}
}
}

pub fn name_is_self(&self) -> bool {
if let Some(name) = self.get_root_path() {
name.name() == "Self"
} else {
false
}
}

pub fn specialize(&self, mappings: &[(&Path, &GenericArgument)]) -> Type {
match *self {
Type::Ptr {
Expand Down
51 changes: 51 additions & 0 deletions src/bindgen/ir/typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,57 @@ impl Typedef {
}
}

pub fn load_impl(
item: &syn::ImplItemType,
mod_cfg: Option<&Cfg>,
trait_ident: &syn::Ident,
self_ty: &Type,
) -> Result<Typedef, String> {
let ty_path = match Type::load(&item.ty) {
Ok(t) => t,
Err(err) => {
return Err(format!(
"Failed to load the type {}::{} {}",
trait_ident, item.ident, err,
));
}
};

let impl_ty_path = match self_ty.get_root_path() {
Some(path) => path,
None => {
return Err(format!(
"Couldn't find path for {self_ty:?}, skipping associated type"
));
}
};

if let Some(ty_path) = ty_path {
let path = Path::new(format!(
"{}_{}_{}",
impl_ty_path.name(),
trait_ident,
item.ident,
));
let aliased = if ty_path.name_is_self() {
self_ty.clone()
} else {
ty_path
};

Ok(Typedef::new(
path,
GenericParams::load(&item.generics)?,
aliased,
Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
AnnotationSet::load(&item.attrs)?,
Documentation::load(&item.attrs),
))
} else {
Err("Cannot have a typedef of a zero sized type.".to_owned())
}
}

pub fn new(
path: Path,
generic_params: GenericParams,
Expand Down
Loading