From b457464dd6935cb97e969a3907bf3464ca4f9ac9 Mon Sep 17 00:00:00 2001 From: Alissa Rao Date: Tue, 12 Apr 2022 02:58:47 -0700 Subject: [PATCH] Use rustfmt to ensure consistant style. --- enumset/src/lib.rs | 120 ++++++++++++++++---------------- enumset/src/repr.rs | 140 +++++++++++++++++++++++++++----------- enumset_derive/src/lib.rs | 41 +++++------ rustfmt.toml | 21 ++++++ 4 files changed, 204 insertions(+), 118 deletions(-) create mode 100644 rustfmt.toml diff --git a/enumset/src/lib.rs b/enumset/src/lib.rs index 384f458..104b9f1 100644 --- a/enumset/src/lib.rs +++ b/enumset/src/lib.rs @@ -1,6 +1,5 @@ #![no_std] #![forbid(missing_docs)] - // The safety requirement is "use the procedural derive". #![allow(clippy::missing_safety_doc)] @@ -95,7 +94,8 @@ pub mod __internal { pub use ::core as core_export; /// A reexport of serde so there is no requirement to depend on serde. - #[cfg(feature = "serde")] pub use serde2 as serde; + #[cfg(feature = "serde")] + pub use serde2 as serde; /// The actual members of EnumSetType. Put here to avoid polluting global namespaces. pub unsafe trait EnumSetTypePrivate { @@ -115,16 +115,18 @@ pub mod __internal { /// can control how `EnumSet` is serialized. #[cfg(feature = "serde")] fn serialize(set: EnumSet, ser: S) -> Result - where Self: EnumSetType; + where Self: EnumSetType; /// Deserializes the `EnumSet`. #[cfg(feature = "serde")] fn deserialize<'de, D: serde::Deserializer<'de>>(de: D) -> Result, D::Error> - where Self: EnumSetType; + where Self: EnumSetType; } } +#[cfg(feature = "serde")] +use crate::__internal::serde; use crate::__internal::EnumSetTypePrivate; -#[cfg(feature = "serde")] use crate::__internal::serde; -#[cfg(feature = "serde")] use crate::serde::{Serialize, Deserialize}; +#[cfg(feature = "serde")] +use crate::serde::{Deserialize, Serialize}; mod repr; use crate::repr::EnumSetTypeRepr; @@ -224,7 +226,7 @@ pub use enumset_derive::EnumSetType; /// /// For full documentation on the procedural derive and its options, see /// [`#[derive(EnumSetType)]`](./derive.EnumSetType.html). -pub unsafe trait EnumSetType: Copy + Eq + EnumSetTypePrivate { } +pub unsafe trait EnumSetType: Copy + Eq + EnumSetTypePrivate {} /// An [`EnumSetType`] for which [`EnumSet`]s have a guaranteed in-memory representation. /// @@ -235,7 +237,9 @@ pub unsafe trait EnumSetType: Copy + Eq + EnumSetTypePrivate { } /// For any type `T` that implements this trait, the in-memory representation of `EnumSet` /// is guaranteed to be `Repr`. This guarantee is useful for FFI. See [the `EnumSet` documentation /// under “FFI, Safety and `repr`”][EnumSet#ffi-safety-and-repr] for an example. -pub unsafe trait EnumSetTypeWithRepr: EnumSetType + EnumSetTypePrivate::Repr> { +pub unsafe trait EnumSetTypeWithRepr: + EnumSetType + EnumSetTypePrivate::Repr> +{ /// The guaranteed representation. type Repr: EnumSetTypeRepr; } @@ -282,8 +286,8 @@ pub unsafe trait EnumSetTypeWithRepr: EnumSetType + EnumSetTypePrivate[EnumSetTypeWithRepr]<Repr = R> in addition to /// [`EnumSetType`]. @@ -329,9 +333,9 @@ pub struct EnumSet { #[doc(hidden)] /// This is public due to the [`enum_set!`] macro. /// This is **NOT** public API and may change at any time. - pub __priv_repr: T::Repr + pub __priv_repr: T::Repr, } -impl EnumSet { +impl EnumSet { // Returns all bits valid for the enum #[inline(always)] fn all_bits() -> T::Repr { @@ -501,9 +505,7 @@ impl EnumSet { /// annotation. #[inline(always)] pub fn as_repr(&self) -> ::Repr - where - T: EnumSetTypeWithRepr, - { + where T: EnumSetTypeWithRepr { self.__priv_repr } @@ -521,9 +523,7 @@ impl EnumSet { /// `T` must be set to 0. Behavior is **undefined** if any of these bits are set to 1. #[inline(always)] pub unsafe fn from_repr_unchecked(bits: ::Repr) -> Self - where - T: EnumSetTypeWithRepr, - { + where T: EnumSetTypeWithRepr { Self { __priv_repr: bits } } @@ -536,9 +536,7 @@ impl EnumSet { /// annotation. #[inline(always)] pub fn from_repr(bits: ::Repr) -> Self - where - T: EnumSetTypeWithRepr, - { + where T: EnumSetTypeWithRepr { Self::try_from_repr(bits).expect("Bitset contains invalid variants.") } @@ -551,9 +549,7 @@ impl EnumSet { /// annotation. #[inline(always)] pub fn try_from_repr(bits: ::Repr) -> Option - where - T: EnumSetTypeWithRepr, - { + where T: EnumSetTypeWithRepr { let mask = Self::all().__priv_repr; if bits.and_not(mask).is_empty() { Some(EnumSet { __priv_repr: bits }) @@ -568,9 +564,7 @@ impl EnumSet { /// annotation. #[inline(always)] pub fn from_repr_truncated(bits: ::Repr) -> Self - where - T: EnumSetTypeWithRepr, - { + where T: EnumSetTypeWithRepr { let mask = Self::all().as_repr(); let bits = bits & mask; EnumSet { __priv_repr: bits } @@ -676,14 +670,14 @@ conversion_impls! { as_usize try_as_usize as_usize_truncated); } -impl Default for EnumSet { +impl Default for EnumSet { /// Returns an empty set. fn default() -> Self { Self::new() } } -impl IntoIterator for EnumSet { +impl IntoIterator for EnumSet { type Item = T; type IntoIter = EnumSetIter; @@ -691,49 +685,49 @@ impl IntoIterator for EnumSet { self.iter() } } -impl Sum for EnumSet { - fn sum>(iter: I) -> Self { +impl Sum for EnumSet { + fn sum>(iter: I) -> Self { iter.fold(EnumSet::empty(), |a, v| a | v) } } -impl <'a, T: EnumSetType> Sum<&'a EnumSet> for EnumSet { - fn sum>(iter: I) -> Self { +impl<'a, T: EnumSetType> Sum<&'a EnumSet> for EnumSet { + fn sum>(iter: I) -> Self { iter.fold(EnumSet::empty(), |a, v| a | *v) } } -impl Sum for EnumSet { - fn sum>(iter: I) -> Self { +impl Sum for EnumSet { + fn sum>(iter: I) -> Self { iter.fold(EnumSet::empty(), |a, v| a | v) } } -impl <'a, T: EnumSetType> Sum<&'a T> for EnumSet { - fn sum>(iter: I) -> Self { +impl<'a, T: EnumSetType> Sum<&'a T> for EnumSet { + fn sum>(iter: I) -> Self { iter.fold(EnumSet::empty(), |a, v| a | *v) } } -impl >> Sub for EnumSet { +impl>> Sub for EnumSet { type Output = Self; #[inline(always)] fn sub(self, other: O) -> Self::Output { self.difference(other.into()) } } -impl >> BitAnd for EnumSet { +impl>> BitAnd for EnumSet { type Output = Self; #[inline(always)] fn bitand(self, other: O) -> Self::Output { self.intersection(other.into()) } } -impl >> BitOr for EnumSet { +impl>> BitOr for EnumSet { type Output = Self; #[inline(always)] fn bitor(self, other: O) -> Self::Output { self.union(other.into()) } } -impl >> BitXor for EnumSet { +impl>> BitXor for EnumSet { type Output = Self; #[inline(always)] fn bitxor(self, other: O) -> Self::Output { @@ -741,32 +735,32 @@ impl >> BitXor for EnumSet { } } -impl >> SubAssign for EnumSet { +impl>> SubAssign for EnumSet { #[inline(always)] fn sub_assign(&mut self, rhs: O) { *self = *self - rhs; } } -impl >> BitAndAssign for EnumSet { +impl>> BitAndAssign for EnumSet { #[inline(always)] fn bitand_assign(&mut self, rhs: O) { *self = *self & rhs; } } -impl >> BitOrAssign for EnumSet { +impl>> BitOrAssign for EnumSet { #[inline(always)] fn bitor_assign(&mut self, rhs: O) { *self = *self | rhs; } } -impl >> BitXorAssign for EnumSet { +impl>> BitXorAssign for EnumSet { #[inline(always)] fn bitxor_assign(&mut self, rhs: O) { *self = *self ^ rhs; } } -impl Not for EnumSet { +impl Not for EnumSet { type Output = Self; #[inline(always)] fn not(self) -> Self::Output { @@ -774,23 +768,25 @@ impl Not for EnumSet { } } -impl From for EnumSet { +impl From for EnumSet { fn from(t: T) -> Self { EnumSet::only(t) } } -impl PartialEq for EnumSet { +impl PartialEq for EnumSet { fn eq(&self, other: &T) -> bool { self.__priv_repr == EnumSet::only(*other).__priv_repr } } -impl Debug for EnumSet { +impl Debug for EnumSet { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { let mut is_first = true; f.write_str("EnumSet(")?; for v in self.iter() { - if !is_first { f.write_str(" | ")?; } + if !is_first { + f.write_str(" | ")?; + } is_first = false; v.fmt(f)?; } @@ -800,31 +796,31 @@ impl Debug for EnumSet { } #[allow(clippy::derive_hash_xor_eq)] // This impl exists to change trait bounds only. -impl Hash for EnumSet { +impl Hash for EnumSet { fn hash(&self, state: &mut H) { self.__priv_repr.hash(state) } } -impl PartialOrd for EnumSet { +impl PartialOrd for EnumSet { fn partial_cmp(&self, other: &Self) -> Option { self.__priv_repr.partial_cmp(&other.__priv_repr) } } -impl Ord for EnumSet { +impl Ord for EnumSet { fn cmp(&self, other: &Self) -> Ordering { self.__priv_repr.cmp(&other.__priv_repr) } } #[cfg(feature = "serde")] -impl Serialize for EnumSet { +impl Serialize for EnumSet { fn serialize(&self, serializer: S) -> Result { T::serialize(*self, serializer) } } #[cfg(feature = "serde")] -impl <'de, T: EnumSetType> Deserialize<'de> for EnumSet { +impl<'de, T: EnumSetType> Deserialize<'de> for EnumSet { fn deserialize>(deserializer: D) -> Result { T::deserialize(deserializer) } @@ -835,13 +831,13 @@ impl <'de, T: EnumSetType> Deserialize<'de> for EnumSet { pub struct EnumSetIter { set: EnumSet, } -impl EnumSetIter { +impl EnumSetIter { fn new(set: EnumSet) -> EnumSetIter { EnumSetIter { set } } } -impl Iterator for EnumSetIter { +impl Iterator for EnumSetIter { type Item = T; fn next(&mut self) -> Option { @@ -859,7 +855,7 @@ impl Iterator for EnumSetIter { } } -impl DoubleEndedIterator for EnumSetIter { +impl DoubleEndedIterator for EnumSetIter { fn next_back(&mut self) -> Option { if self.set.is_empty() { None @@ -875,7 +871,9 @@ impl ExactSizeIterator for EnumSetIter {} impl Extend for EnumSet { fn extend>(&mut self, iter: I) { - iter.into_iter().for_each(|v| { self.insert(v); }); + iter.into_iter().for_each(|v| { + self.insert(v); + }); } } @@ -889,7 +887,9 @@ impl FromIterator for EnumSet { impl Extend> for EnumSet { fn extend>>(&mut self, iter: I) { - iter.into_iter().for_each(|v| { self.insert_all(v); }); + iter.into_iter().for_each(|v| { + self.insert_all(v); + }); } } diff --git a/enumset/src/repr.rs b/enumset/src/repr.rs index 7dfa89b..38546aa 100644 --- a/enumset/src/repr.rs +++ b/enumset/src/repr.rs @@ -1,6 +1,6 @@ use core::convert::TryInto; -use core::fmt::{Debug}; -use core::hash::{Hash}; +use core::fmt::Debug; +use core::hash::Hash; use core::ops::*; /// A trait marking valid underlying bitset storage types and providing the @@ -68,9 +68,13 @@ macro_rules! prim { const WIDTH: u32 = $width; #[inline(always)] - fn is_empty(&self) -> bool { *self == 0 } + fn is_empty(&self) -> bool { + *self == 0 + } #[inline(always)] - fn empty() -> Self { 0 } + fn empty() -> Self { + 0 + } #[inline(always)] fn add_bit(&mut self, bit: u32) { @@ -86,78 +90,136 @@ macro_rules! prim { } #[inline(always)] - fn count_ones(&self) -> u32 { (*self).count_ones() } + fn count_ones(&self) -> u32 { + (*self).count_ones() + } #[inline(always)] - fn leading_zeros(&self) -> u32 { (*self).leading_zeros() } + fn leading_zeros(&self) -> u32 { + (*self).leading_zeros() + } #[inline(always)] - fn trailing_zeros(&self) -> u32 { (*self).trailing_zeros() } + fn trailing_zeros(&self) -> u32 { + (*self).trailing_zeros() + } #[inline(always)] - fn and_not(&self, other: Self) -> Self { (*self) & !other } + fn and_not(&self, other: Self) -> Self { + (*self) & !other + } #[inline(always)] fn count_remaining_ones(&self, cursor: u32) -> usize { - let left_mask = - !((1 as $name).checked_shl(cursor).unwrap_or(0).wrapping_sub(1)); + let left_mask = !((1 as $name) + .checked_shl(cursor) + .unwrap_or(0) + .wrapping_sub(1)); (*self & left_mask).count_ones() as usize } #[inline(always)] - fn from_u8(v: u8) -> Self { v as $name } + fn from_u8(v: u8) -> Self { + v as $name + } #[inline(always)] - fn from_u16(v: u16) -> Self { v as $name } + fn from_u16(v: u16) -> Self { + v as $name + } #[inline(always)] - fn from_u32(v: u32) -> Self { v as $name } + fn from_u32(v: u32) -> Self { + v as $name + } #[inline(always)] - fn from_u64(v: u64) -> Self { v as $name } + fn from_u64(v: u64) -> Self { + v as $name + } #[inline(always)] - fn from_u128(v: u128) -> Self { v as $name } + fn from_u128(v: u128) -> Self { + v as $name + } #[inline(always)] - fn from_usize(v: usize) -> Self { v as $name } + fn from_usize(v: usize) -> Self { + v as $name + } #[inline(always)] - fn to_u8(&self) -> u8 { (*self) as u8 } + fn to_u8(&self) -> u8 { + (*self) as u8 + } #[inline(always)] - fn to_u16(&self) -> u16 { (*self) as u16 } + fn to_u16(&self) -> u16 { + (*self) as u16 + } #[inline(always)] - fn to_u32(&self) -> u32 { (*self) as u32 } + fn to_u32(&self) -> u32 { + (*self) as u32 + } #[inline(always)] - fn to_u64(&self) -> u64 { (*self) as u64 } + fn to_u64(&self) -> u64 { + (*self) as u64 + } #[inline(always)] - fn to_u128(&self) -> u128 { (*self) as u128 } + fn to_u128(&self) -> u128 { + (*self) as u128 + } #[inline(always)] - fn to_usize(&self) -> usize { (*self) as usize } + fn to_usize(&self) -> usize { + (*self) as usize + } #[inline(always)] - fn from_u8_opt(v: u8) -> Option { v.try_into().ok() } + fn from_u8_opt(v: u8) -> Option { + v.try_into().ok() + } #[inline(always)] - fn from_u16_opt(v: u16) -> Option { v.try_into().ok() } + fn from_u16_opt(v: u16) -> Option { + v.try_into().ok() + } #[inline(always)] - fn from_u32_opt(v: u32) -> Option { v.try_into().ok() } + fn from_u32_opt(v: u32) -> Option { + v.try_into().ok() + } #[inline(always)] - fn from_u64_opt(v: u64) -> Option { v.try_into().ok() } + fn from_u64_opt(v: u64) -> Option { + v.try_into().ok() + } #[inline(always)] - fn from_u128_opt(v: u128) -> Option { v.try_into().ok() } + fn from_u128_opt(v: u128) -> Option { + v.try_into().ok() + } #[inline(always)] - fn from_usize_opt(v: usize) -> Option { v.try_into().ok() } + fn from_usize_opt(v: usize) -> Option { + v.try_into().ok() + } #[inline(always)] - fn to_u8_opt(&self) -> Option { (*self).try_into().ok() } + fn to_u8_opt(&self) -> Option { + (*self).try_into().ok() + } #[inline(always)] - fn to_u16_opt(&self) -> Option { (*self).try_into().ok() } + fn to_u16_opt(&self) -> Option { + (*self).try_into().ok() + } #[inline(always)] - fn to_u32_opt(&self) -> Option { (*self).try_into().ok() } + fn to_u32_opt(&self) -> Option { + (*self).try_into().ok() + } #[inline(always)] - fn to_u64_opt(&self) -> Option { (*self).try_into().ok() } + fn to_u64_opt(&self) -> Option { + (*self).try_into().ok() + } #[inline(always)] - fn to_u128_opt(&self) -> Option { (*self).try_into().ok() } + fn to_u128_opt(&self) -> Option { + (*self).try_into().ok() + } #[inline(always)] - fn to_usize_opt(&self) -> Option { (*self).try_into().ok() } + fn to_usize_opt(&self) -> Option { + (*self).try_into().ok() + } } - } + }; } -prim!(u8 , 8 ); -prim!(u16 , 16 ); -prim!(u32 , 32 ); -prim!(u64 , 64 ); +prim!(u8, 8); +prim!(u16, 16); +prim!(u32, 32); +prim!(u64, 64); prim!(u128, 128); diff --git a/enumset_derive/src/lib.rs b/enumset_derive/src/lib.rs index 7e117f0..5e1afa0 100644 --- a/enumset_derive/src/lib.rs +++ b/enumset_derive/src/lib.rs @@ -1,14 +1,14 @@ -#![recursion_limit="256"] +#![recursion_limit = "256"] extern crate proc_macro; use darling::*; use proc_macro::TokenStream; -use proc_macro2::{TokenStream as SynTokenStream, Literal, Span}; +use proc_macro2::{Literal, Span, TokenStream as SynTokenStream}; +use quote::*; use std::{collections::HashSet, fmt::Display}; -use syn::{*, Result, Error}; use syn::spanned::Spanned; -use quote::*; +use syn::{Error, Result, *}; /// Helper function for emitting compile errors. fn error(span: Span, message: impl Display) -> Result { @@ -81,7 +81,9 @@ impl EnumSetInfo { name: input.ident.clone(), crate_name: attrs.crate_name.map(|x| Ident::new(&x, Span::call_site())), explicit_mem_repr: attrs.repr.map(|x| Ident::new(&x, Span::call_site())), - explicit_serde_repr: attrs.serialize_repr.map(|x| Ident::new(&x, Span::call_site())), + explicit_serde_repr: attrs + .serialize_repr + .map(|x| Ident::new(&x, Span::call_site())), has_signed_repr: false, has_large_repr: false, variants: Vec::new(), @@ -92,7 +94,7 @@ impl EnumSetInfo { no_ops: attrs.no_ops, no_super_impls: attrs.no_super_impls, serialize_as_list: attrs.serialize_as_list, - serialize_deny_unknown: attrs.serialize_deny_unknown + serialize_deny_unknown: attrs.serialize_deny_unknown, } } @@ -115,7 +117,7 @@ impl EnumSetInfo { self.has_large_repr = true; Ok(()) } - _ => error(attr_span, "Unsupported repr.") + _ => error(attr_span, "Unsupported repr."), } } /// Adds a variant to the enumset. @@ -171,10 +173,8 @@ impl EnumSetInfo { if discriminant > self.max_discrim { self.max_discrim = discriminant; } - self.variants.push(EnumSetValue { - name: variant.ident.clone(), - variant_repr: discriminant, - }); + self.variants + .push(EnumSetValue { name: variant.ident.clone(), variant_repr: discriminant }); self.used_variant_names.insert(variant.ident.to_string()); self.used_discriminants.insert(discriminant); @@ -195,7 +195,7 @@ impl EnumSetInfo { "u128" => self.max_discrim >= 128, _ => error( Span::call_site(), - "Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for serde_repr." + "Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for serde_repr.", )?, }; if is_overflowed { @@ -212,7 +212,7 @@ impl EnumSetInfo { "u128" => self.max_discrim >= 128, _ => error( Span::call_site(), - "Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for repr." + "Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for repr.", )?, }; if is_overflowed { @@ -286,7 +286,7 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream { { quote!(::enumset) } - }, + } }; let typed_enumset = quote!(#enumset::EnumSet<#name>); let core = quote!(#enumset::__internal::core_export); @@ -336,7 +336,6 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream { } }; - #[cfg(feature = "serde")] let serde = quote!(#enumset::__internal::serde); @@ -392,7 +391,7 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream { } } } else { - quote! { } + quote! {} }; quote! { fn serialize( @@ -413,7 +412,7 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream { }; #[cfg(not(feature = "serde"))] - let serde_ops = quote! { }; + let serde_ops = quote! {}; let is_uninhabited = info.variants.is_empty(); let is_zst = info.variants.len() == 1; @@ -441,9 +440,13 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream { let variant_value: Vec<_> = info.variants.iter().map(|x| x.variant_repr).collect(); let const_field: Vec<_> = ["IS_U8", "IS_U16", "IS_U32", "IS_U64", "IS_U128"] - .iter().map(|x| Ident::new(x, Span::call_site())).collect(); + .iter() + .map(|x| Ident::new(x, Span::call_site())) + .collect(); let int_type: Vec<_> = ["u8", "u16", "u32", "u64", "u128"] - .iter().map(|x| Ident::new(x, Span::call_site())).collect(); + .iter() + .map(|x| Ident::new(x, Span::call_site())) + .collect(); quote! { fn enum_into_u32(self) -> u32 { diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..319b323 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,21 @@ +# Width options +max_width = 100 +fn_call_width = 80 +attr_fn_like_width = 80 +struct_lit_width = 80 +struct_variant_width = 60 +array_width = 80 +chain_width = 60 +single_line_if_else_max_width = 60 + +# Other options +merge_derives = false +overflow_delimited_expr = true +struct_lit_single_line = true +empty_item_single_line = true +where_single_line = true + +# Ignored paths +ignore = [ + "enumset/tests", +] \ No newline at end of file -- 2.44.0