Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for the internal Field0 and examples of formatting/parsing #328

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,43 @@
//! text-based representation for flags that generated flags types can use. For details on the exact
//! grammar, see the [`parser`] module.
//!
//! To support formatting and parsing your generated flags types, you can implement the standard `Display`
KodrAus marked this conversation as resolved.
Show resolved Hide resolved
//! and `FromStr` traits:
//!
//! ```
//! use bitflags::bitflags;
//! use std::{fmt, str};
//!
//! bitflags::bitflags! {
//! pub struct Flags: u32 {
//! const A = 1;
//! const B = 2;
//! const C = 4;
//! const D = 8;
//! }
//! }
//!
//! impl fmt::Debug for Flags {
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//! fmt::Debug::fmt(&self.0, f)
//! }
//! }
//!
//! impl fmt::Display for Flags {
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//! fmt::Display::fmt(&self.0, f)
//! }
//! }
//!
//! impl str::FromStr for Flags {
//! type Err = bitflags::parser::ParseError;
//!
//! fn from_str(flags: &str) -> Result<Self, Self::Err> {
//! Ok(Self(flags.parse()?))
//! }
//! }
//! ```
//!
//! ## `PartialEq` and `PartialOrd`
//!
//! Equality and ordering can be derived for a reasonable implementation, or implemented manually
Expand Down Expand Up @@ -342,6 +379,19 @@
//!
//! assert_eq!(2, count_unset_flags(&Flags::B));
//! ```
//!
//! # The internal field
//!
//! This library generates newtypes like:
//!
//! ```
//! # pub struct Field0;
//! pub struct Flags(Field0);
//! ```
//!
//! You can freely use methods and trait implementations on this internal field as `.0`.
//! For details on exactly what's generated for it, see the [`Field0`](example_generated/struct.Field0.html)
//! example docs.

#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![doc(html_root_url = "https://docs.rs/bitflags/2.0.2")]
Expand Down