Skip to content
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
39 changes: 35 additions & 4 deletions foundations-macros/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{
parse_macro_input, parse_quote, Attribute, Expr, ExprLit, Field, Fields, Ident, Item, ItemEnum,
ItemStruct, Lit, LitStr, Meta, MetaNameValue, Path,
ItemStruct, Lit, LitStr, Meta, MetaNameValue, Path, Type,
};

const ERR_NOT_STRUCT_OR_ENUM: &str = "Settings should be either structure or enum.";
Expand Down Expand Up @@ -238,12 +238,19 @@ fn impl_settings_trait_for_field(

impl_for_field.append_all(quote_spanned! { span=>
let mut key = parent_key.to_vec();

key.push(#name_str.into());

#crate_path::settings::Settings::add_docs(&self.#name, &key, docs);
});

// foundations#150: `[T; 0]` used to impl Settings for `T: !Default`, but this
// is not possible anymore. We thus can't call `Settings::add_docs` for such
// fields, but it was a noop anyway.
// TODO(lblocher): Remove this compatibility hack with the next major release
if !is_array_zst(&field.ty) {
impl_for_field.append_all(quote_spanned! { span =>
#crate_path::settings::Settings::add_docs(&self.#name, &key, docs);
});
}

if !docs.is_empty() {
impl_for_field.append_all(quote! {
docs.insert(key, &[#(#docs,)*][..]);
Expand Down Expand Up @@ -286,6 +293,30 @@ fn extract_doc_comments(attrs: &[Attribute]) -> Vec<LitStr> {
comments
}

/// Returns whether `ty` is exactly `[T; 0]` (for some T)
fn is_array_zst(ty: &Type) -> bool {
let Type::Array(array) = ty else {
return false;
};

let mut expr = &array.len;
while let Expr::Cast(cast) = expr {
expr = &cast.expr;
}

let Expr::Lit(ExprLit {
lit: Lit::Int(len_lit),
..
}) = expr
else {
return false;
};
len_lit
.base10_parse::<usize>()
.map(|len| len == 0)
.unwrap_or(false)
}

fn impl_serde_aware_default(item: &ItemStruct) -> proc_macro2::TokenStream {
let name = &item.ident;
let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();
Expand Down
9 changes: 4 additions & 5 deletions foundations/src/settings/basic_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ macro_rules! impl_noop {
}

impl_noop!(<T> Settings for PhantomData<T> where T: 'static);
impl_noop!(<T> Settings for [T; 0] where T: Debug + Clone + 'static);
impl_noop!(<Idx> Settings for Range<Idx> where Idx: Debug + Serialize + DeserializeOwned + Clone + Default + 'static);
impl_noop!(<T> Settings for Reverse<T> where T: Settings);
impl_noop!(<T> Settings for Wrapping<T> where T: Settings);
Expand Down Expand Up @@ -119,10 +118,10 @@ macro_rules! impl_for_array {
}

impl_for_array! {
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32
}

impl<T: Settings> Settings for Option<T> {
Expand Down