/// Get the "shape" of a fields container, such as a struct or variant. pubtrait AsShape { /// Get the "shape" of a fields container. fn as_shape(&self) -> Shape;
}
/// Description of how fields in a struct or variant are syntactically laid out. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pubenum Shape { /// A set of named fields, e.g. `{ field: String }`.
Named, /// A list of unnamed fields, e.g. `(String, u64)`.
Tuple, /// No fields, e.g. `struct Example;`
Unit, /// A special case of [`Tuple`](Shape#variant.Tuple) with exactly one field, e.g. `(String)`.
Newtype,
}
/// A set of [`Shape`] values, which correctly handles the relationship between /// [newtype](Shape#variant.Newtype) and [tuple](Shape#variant.Tuple) shapes. /// /// # Example /// ```rust /// # use darling_core::util::{Shape, ShapeSet}; /// let shape_set = ShapeSet::new(vec![Shape::Tuple]); /// /// // This is correct, because all newtypes are single-field tuples. /// assert!(shape_set.contains(&Shape::Newtype)); /// ``` #[derive(Debug, Clone, Default)] pubstruct ShapeSet {
newtype: bool,
named: bool,
tuple: bool,
unit: bool,
}
impl ShapeSet { /// Create a new `ShapeSet` which includes the specified items. /// /// # Exampe /// ```rust /// # use darling_core::util::{Shape, ShapeSet}; /// let shape_set = ShapeSet::new(vec![Shape::Named, Shape::Newtype]); /// assert!(shape_set.contains(&Shape::Newtype)); /// ``` pubfn new(items: impl IntoIterator<Item = Shape>) -> Self {
items.into_iter().collect()
}
/// Insert all possible shapes into the set. /// /// This is equivalent to calling [`insert`](ShapeSet#method.insert) with every value of [`Shape`]. /// /// # Example /// ```rust /// # use darling_core::util::{Shape, ShapeSet}; /// let mut shape_set = ShapeSet::default(); /// shape_set.insert_all(); /// assert!(shape_set.contains(&Shape::Named)); /// ``` pubfn insert_all(&mutself) { self.insert(Shape::Named); self.insert(Shape::Newtype); self.insert(Shape::Tuple); self.insert(Shape::Unit);
}
/// Insert a shape into the set, so that the set will match that shape pubfn insert(&mutself, shape: Shape) { match shape {
Shape::Named => self.named = true,
Shape::Tuple => self.tuple = true,
Shape::Unit => self.unit = true,
Shape::Newtype => self.newtype = true,
}
}
/// Whether this set is empty. pubfn is_empty(&self) -> bool {
!self.named && !self.newtype && !self.tuple && !self.unit
}
/// Check if a fields container's shape is in this set. pubfn contains(&self, fields: &impl AsShape) -> bool { self.contains_shape(fields.as_shape())
}
/// Check if a field container's shape is in this set of shapes, and produce /// an [`Error`](crate::Error) if it does not. pubfn check(&self, fields: &impl AsShape) -> crate::Result<()> { let shape = fields.as_shape();
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.