Skip to content

Commit

Permalink
Feature: naive support for const param generics. (#106)
Browse files Browse the repository at this point in the history
* Feature: const generic parameter.

* Feature: support for const generic parameters.

* Fix: removing dummy expand.rs generated in testing.

* Use an imported type in the example.

---------

Co-authored-by: y-jiji <y.jijiji.data.science@gmail.com>
Co-authored-by: joshua-maros <60271685+joshua-maros@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 10, 2023
1 parent e60d426 commit 1383a6a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 10 additions & 0 deletions examples/src/lib.rs
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
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

0 comments on commit 1383a6a

Please sign in to comment.