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

Feature: naive support for const param generics. #106

Merged
merged 4 commits into from
Dec 10, 2023
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
10 changes: 10 additions & 0 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ use ouroboros::self_referencing;
#[cfg(test)]
mod ok_tests;

pub struct Ext<'this, T, const REV: bool>(&'this Box<T>);

#[self_referencing(pub_extras)]
pub struct WithConstParam<T: 'static, const REV: bool> {
data: core::cell::RefCell<T>,
#[borrows(data)]
#[not_covariant]
dref: Option<Box<Ext<'this, T, REV>>>,
}

#[self_referencing]
/// A simple struct which contains an `i32` and a `&'this i32`.
pub struct DataAndRef {
Expand Down
17 changes: 15 additions & 2 deletions ouroboros_macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ pub fn make_generic_consumers(generics: &Generics) -> impl Iterator<Item = (Toke
format_ident!("_consume_template_lifetime_{}", ident),
)
}
GenericParam::Const(..) => unimplemented!(),
// rustc don't require constants to consume, so we just skip it.
GenericParam::Const(ct) => {
let ident = ct.ident;
(
quote! { () },
format_ident!(
"_comsume_template_const_parameter_{}",
ident.to_string().to_snake_case()
),
)
},
})
}

Expand All @@ -45,7 +55,10 @@ pub fn make_generic_arguments(generics: Vec<&GenericParam>) -> Vec<TokenStream>
let lifetime = &lt.lifetime;
arguments.push(quote! { #lifetime });
}
GenericParam::Const(_) => unimplemented!("Const generics are not supported yet."),
GenericParam::Const(ct) => {
let ident = &ct.ident;
arguments.push(quote! { #ident });
},
}
}
arguments
Expand Down
Loading