From 1383a6ae8d34a53e2b1b5d22a130def17807618b Mon Sep 17 00:00:00 2001 From: Andy Yang <71013200+Y-jiji@users.noreply.github.com> Date: Mon, 11 Dec 2023 01:22:18 +0800 Subject: [PATCH] Feature: naive support for const param generics. (#106) * 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 Co-authored-by: joshua-maros <60271685+joshua-maros@users.noreply.github.com> --- examples/src/lib.rs | 10 ++++++++++ ouroboros_macro/src/utils.rs | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 3198651..df5475d 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -8,6 +8,16 @@ use ouroboros::self_referencing; #[cfg(test)] mod ok_tests; +pub struct Ext<'this, T, const REV: bool>(&'this Box); + +#[self_referencing(pub_extras)] +pub struct WithConstParam { + data: core::cell::RefCell, + #[borrows(data)] + #[not_covariant] + dref: Option>>, +} + #[self_referencing] /// A simple struct which contains an `i32` and a `&'this i32`. pub struct DataAndRef { diff --git a/ouroboros_macro/src/utils.rs b/ouroboros_macro/src/utils.rs index ec527df..c62ad29 100644 --- a/ouroboros_macro/src/utils.rs +++ b/ouroboros_macro/src/utils.rs @@ -28,7 +28,17 @@ pub fn make_generic_consumers(generics: &Generics) -> impl Iterator 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() + ), + ) + }, }) } @@ -45,7 +55,10 @@ pub fn make_generic_arguments(generics: Vec<&GenericParam>) -> Vec let lifetime = <.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