// Copyright 2019 The Fuchsia Authors // // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. // This file may not be copied, modified, or distributed except according to // those terms.
pubstruct Config<Repr: KindRepr> { // A human-readable message describing what combinations of representations // are allowed. This will be printed to the user if they use an invalid // combination. pub allowed_combinations_message: &'static str, // Whether we're checking as part of `derive(Unaligned)`. If not, we can // ignore `repr(align)`, which makes the code (and the list of valid repr // combinations we have to enumerate) somewhat simpler. If we're checking // for `Unaligned`, then in addition to checking against illegal // combinations, we also check to see if there exists a `repr(align(N > 1))` // attribute. pub derive_unaligned: bool, // Combinations which are valid for the trait. pub allowed_combinations: &'static [&'static [Repr]], // Combinations which are not valid for the trait, but are legal according // to Rust. Any combination not in this or `allowed_combinations` is either // illegal according to Rust or the behavior is unspecified. If the behavior // is unspecified, it might become specified in the future, and that // specification might not play nicely with our requirements. Thus, we // reject combinations with unspecified behavior in addition to illegal // combinations. pub disallowed_but_legal_combinations: &'static [&'static [Repr]],
}
impl<R: KindRepr> Config<R> { /// Validate that `input`'s representation attributes conform to the /// requirements specified by this `Config`. /// /// `validate_reprs` extracts the `repr` attributes, validates that they /// conform to the requirements of `self`, and returns them. Regardless of /// whether `align` attributes are considered during validation, they are /// stripped out of the returned value since no callers care about them. pubfn validate_reprs(&self, input: &DeriveInput) -> Result<Vec<R>, Vec<Error>> { letmut metas_reprs = reprs(&input.attrs)?;
metas_reprs.sort_by(|a: &(_, R), b| a.1.partial_cmp(&b.1).unwrap());
if reprs.is_empty() { // Use `Span::call_site` to report this error on the // `#[derive(...)]` itself. return Err(vec![Error::new(Span::call_site(), "must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout")]);
}
let initial_sp = metas[0].span(); let err_span = metas.iter().skip(1).try_fold(initial_sp, |sp, meta| sp.join(meta.span()));
// The type of valid reprs for a particular kind (enum, struct, union). pubtrait KindRepr: 'static + Sized + Ord { fn is_align(&self) -> bool; fn is_align_gt_one(&self) -> bool; fn parse(meta: &Meta) -> syn::Result<Self>;
}
// Defines an enum for reprs which are valid for a given kind (structs, enums, // etc), and provide implementations of `KindRepr`, `Ord`, and `Display`, and // those traits' super-traits.
macro_rules! define_kind_specific_repr {
($type_name:expr, $repr_name:ident, [ $($repr_variant:ident),* ] , [ $($repr_variant_aligned:ident),* ]) => { #[derive(Copy, Clone, Debug, Eq, PartialEq)] pubenum $repr_name {
$($repr_variant,)*
$($repr_variant_aligned(u64),)*
}
fn parse(meta: &Meta) -> syn::Result<$repr_name> { match Repr::from_meta(meta)? {
$(Repr::$repr_variant => Ok($repr_name::$repr_variant),)*
$(Repr::$repr_variant_aligned(u) => Ok($repr_name::$repr_variant_aligned(u)),)*
_ => Err(Error::new_spanned(meta, concat!("unsupported representation for deriving FromBytes, AsBytes, or Unaligned on ", $type_name)))
}
}
}
// Define a stable ordering so we can canonicalize lists of reprs. The // ordering itself doesn't matter so long as it's stable. impl PartialOrd for $repr_name { fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for $repr_name { fn cmp(&self, other: &Self) -> core::cmp::Ordering {
format!("{:?}", self).cmp(&format!("{:?}", other))
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.