From: Alissa Rao Date: Tue, 12 Apr 2022 10:03:32 +0000 (-0700) Subject: Unify some redundant code between the variant count checks for repr/serialize_repr X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=b3ce504b4723fc16abb726a238ddedbd56b09046;p=enumset.git Unify some redundant code between the variant count checks for repr/serialize_repr --- diff --git a/enumset/tests/compile-fail/variants.stderr b/enumset/tests/compile-fail/variants.stderr index 0425ae5..51dbd14 100644 --- a/enumset/tests/compile-fail/variants.stderr +++ b/enumset/tests/compile-fail/variants.stderr @@ -61,8 +61,8 @@ error[E0277]: the trait bound `OkayEnumButCantUseFromRepr: EnumSetTypeWithRepr` note: required by a bound in `enumset::EnumSet::::from_repr` --> src/lib.rs | - | T: EnumSetTypeWithRepr, - | ^^^^^^^^^^^^^^^^^^^ required by this bound in `enumset::EnumSet::::from_repr` + | where T: EnumSetTypeWithRepr { + | ^^^^^^^^^^^^^^^^^^^ required by this bound in `enumset::EnumSet::::from_repr` error[E0277]: the trait bound `OkayEnumButCantUseFromRepr: EnumSetTypeWithRepr` is not satisfied --> tests/compile-fail/variants.rs:64:54 diff --git a/enumset_derive/src/lib.rs b/enumset_derive/src/lib.rs index 5e1afa0..cef5a39 100644 --- a/enumset_derive/src/lib.rs +++ b/enumset_derive/src/lib.rs @@ -185,39 +185,32 @@ impl EnumSetInfo { } /// Validate the enumset type. fn validate(&self) -> Result<()> { - // Check if all bits of the bitset can fit in the serialization representation. - if let Some(explicit_serde_repr) = &self.explicit_serde_repr { - let is_overflowed = match explicit_serde_repr.to_string().as_str() { - "u8" => self.max_discrim >= 8, - "u16" => self.max_discrim >= 16, - "u32" => self.max_discrim >= 32, - "u64" => self.max_discrim >= 64, - "u128" => self.max_discrim >= 128, + fn do_check(ty: &str, max_discrim: u32, what: &str) -> Result<()> { + let is_overflowed = match ty { + "u8" => max_discrim >= 8, + "u16" => max_discrim >= 16, + "u32" => max_discrim >= 32, + "u64" => max_discrim >= 64, + "u128" => max_discrim >= 128, _ => error( Span::call_site(), - "Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for serde_repr.", + format!("Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for {what}."), )?, }; if is_overflowed { - error(Span::call_site(), "serialize_repr cannot be smaller than bitset.")?; + error(Span::call_site(), format!("{what} cannot be smaller than bitset."))?; } + Ok(()) + } + + // Check if all bits of the bitset can fit in the serialization representation. + if let Some(explicit_serde_repr) = &self.explicit_serde_repr { + do_check(&explicit_serde_repr.to_string(), self.max_discrim, "serialize_repr")?; } + // Check if all bits of the bitset can fit in the memory representation, if one was given. if let Some(explicit_mem_repr) = &self.explicit_mem_repr { - let is_overflowed = match explicit_mem_repr.to_string().as_str() { - "u8" => self.max_discrim >= 8, - "u16" => self.max_discrim >= 16, - "u32" => self.max_discrim >= 32, - "u64" => self.max_discrim >= 64, - "u128" => self.max_discrim >= 128, - _ => error( - Span::call_site(), - "Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for repr.", - )?, - }; - if is_overflowed { - error(Span::call_site(), "repr cannot be smaller than bitset.")?; - } + do_check(&explicit_mem_repr.to_string(), self.max_discrim, "repr")?; } Ok(()) }