pub struct ByteSet(/* private fields */);Expand description
An efficient, general-purpose set of u8s.
§Implementation
This is a 256-bit mask where a byte is contained based on whether its bit is enabled. The first (least significant) bit in the mask represents the first byte in the set. Likewise, the last last (most significant) bit represents the last byte.
The mask is composed a of “chunk” array. Each chunk is either 64 or 32 bits wide, depending on the target architecture. As of right now, this is based on native register size. This may change in the future based on target features that enable better performance.
Implementations§
Source§impl ByteSet
impl ByteSet
Sourcepub const fn from_byte(byte: u8) -> Self
pub const fn from_byte(byte: u8) -> Self
Returns a set containing only byte.
§Examples
let byte = 42;
let set = ByteSet::from_byte(byte);
assert_eq!(set.first(), Some(byte));
assert_eq!(set.last(), Some(byte));Sourcepub const fn from_range_to(range: RangeTo<u8>) -> Self
pub const fn from_range_to(range: RangeTo<u8>) -> Self
Construct a ByteSet from a RangeTo value, i.e. ..x
Sourcepub const fn from_range_to_inclusive(range: RangeToInclusive<u8>) -> Self
pub const fn from_range_to_inclusive(range: RangeToInclusive<u8>) -> Self
Construct a ByteSet from a RangeToInclusive value, i.e. ..=x
Sourcepub const fn from_range_from(range: RangeFrom<u8>) -> Self
pub const fn from_range_from(range: RangeFrom<u8>) -> Self
Construct a ByteSet from a RangeFrom value, i.e. x..
Sourcepub const fn from_range(range: Range<u8>) -> Self
pub const fn from_range(range: Range<u8>) -> Self
Construct a ByteSet from a RangeToInclusive value, i.e. x..y
Sourcepub const fn from_range_inclusive(range: RangeInclusive<u8>) -> Self
pub const fn from_range_inclusive(range: RangeInclusive<u8>) -> Self
Construct a ByteSet from a RangeInclusive value, i.e. x..=y
Sourcepub fn rand<R: RngCore>(rng: R) -> Self
Available on crate features rand or rand_core only.
pub fn rand<R: RngCore>(rng: R) -> Self
rand or rand_core only.Returns a set containing uniformly-distributed random bytes from rng.
This uses fill_bytes under the hood.
Sourcepub fn try_rand<R: RngCore>(rng: R) -> Result<Self, Error>
Available on crate features rand or rand_core only.
pub fn try_rand<R: RngCore>(rng: R) -> Result<Self, Error>
rand or rand_core only.Returns a set containing uniformly-distributed random bytes from rng,
or Err if rng failed.
This uses try_fill_bytes under the hood.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if self contains no bytes.
This is more efficient than checking self.len() == 0.
Sourcepub const fn is_full(&self) -> bool
pub const fn is_full(&self) -> bool
Returns true if self contains all bytes.
This is more efficient than checking self.len() == 256.
Sourcepub fn first(&self) -> Option<u8>
pub fn first(&self) -> Option<u8>
Returns the first (least) byte in self, or None if self is empty.
Sourcepub fn pop_first(&mut self) -> Option<u8>
pub fn pop_first(&mut self) -> Option<u8>
Removes the first (least) byte in self and returns it, or None if
self is empty.
Sourcepub fn last(&self) -> Option<u8>
pub fn last(&self) -> Option<u8>
Returns the last (greatest) byte in self, or None if self is
empty.
Sourcepub fn pop_last(&mut self) -> Option<u8>
pub fn pop_last(&mut self) -> Option<u8>
Removes the last (least) byte in self and returns it, or None if
self is empty.
Sourcepub fn insert(&mut self, byte: u8)
pub fn insert(&mut self, byte: u8)
Inserts byte into self in-place.
Unlike HashSet::insert and BTreeSet::insert, this does not
return a bool for whether byte was not present. This is because it’s
just as efficient to call contains before.
Sourcepub fn insert_all(&mut self, other: Self)
pub fn insert_all(&mut self, other: Self)
Inserts all bytes of other into self in-place.
Sourcepub const fn inserting_all(self, other: Self) -> Self
pub const fn inserting_all(self, other: Self) -> Self
Returns a copy of self with all of other inserted.
This is equivalent to the union method.
Sourcepub fn remove(&mut self, byte: u8)
pub fn remove(&mut self, byte: u8)
Removes byte from self in-place.
Unlike HashSet::remove and BTreeSet::remove, this does not
return a bool for whether byte was present. This is because it’s
just as efficient to call contains before.
Sourcepub fn remove_all(&mut self, other: Self)
pub fn remove_all(&mut self, other: Self)
Removes all bytes of other from self in-place.
Sourcepub const fn removing_all(self, other: Self) -> Self
pub const fn removing_all(self, other: Self) -> Self
Returns a copy of self with byte removed.
Sourcepub const fn setting(self, byte: u8, enabled: bool) -> Self
pub const fn setting(self, byte: u8, enabled: bool) -> Self
Returns a copy of self with byte set to enabled.
Sourcepub fn contains_any(&self, other: &Self) -> bool
pub fn contains_any(&self, other: &Self) -> bool
Returns true if self contains any bytes in other.
Sourcepub fn is_subset(&self, other: &Self) -> bool
pub fn is_subset(&self, other: &Self) -> bool
Returns true if other contains all bytes in self.
Sourcepub fn is_strict_subset(&self, other: &Self) -> bool
pub fn is_strict_subset(&self, other: &Self) -> bool
Returns true if other contains all bytes in self and at least one
other byte not contained in self.
This is also known as a “proper subset”.
Sourcepub fn is_superset(&self, other: &Self) -> bool
pub fn is_superset(&self, other: &Self) -> bool
Returns true if self contains all bytes in other.
Sourcepub fn is_strict_superset(&self, other: &Self) -> bool
pub fn is_strict_superset(&self, other: &Self) -> bool
Returns true if self contains all bytes in other and at least one
other byte not contained in other.
This is also known as a “proper superset”.
Sourcepub fn is_disjoint(&self, other: &Self) -> bool
pub fn is_disjoint(&self, other: &Self) -> bool
Returns true if self and other have no bytes in common.
Sourcepub const fn difference(self, other: Self) -> Self
pub const fn difference(self, other: Self) -> Self
Returns a set with the bytes contained in self, but not in other.
Sourcepub const fn symmetric_difference(self, other: Self) -> Self
pub const fn symmetric_difference(self, other: Self) -> Self
Returns a set with the bytes contained in self or other, but not in
both.
Sourcepub const fn intersection(self, other: Self) -> Self
pub const fn intersection(self, other: Self) -> Self
Returns a set with the bytes contained both in self and other.
Sourcepub const fn union(self, other: Self) -> Self
pub const fn union(self, other: Self) -> Self
Returns a new set with the bytes contained in self or other.
Sourcepub const fn not(self) -> Self
pub const fn not(self) -> Self
Returns a new set with the bytes not contained in self.
This exists because the Not trait cannot be used in const yet.
Sourcepub const fn reverse_bits(self) -> Self
pub const fn reverse_bits(self) -> Self
Returns self with its bits reversed.
This is equivalent to checking for 255 - b in all subsequent searches
of b.
Source§impl ByteSet
Operations related to the ASCII character set.
impl ByteSet
Operations related to the ASCII character set.
Sourcepub const ASCII: Self
pub const ASCII: Self
The set of all ASCII characters: U+0000 NULL ..= U+007F DELETE.
§Examples
This contains all bytes for which u8::is_ascii returns true:
for byte in ByteSet::ASCII {
assert!(byte.is_ascii());
}
for byte in !ByteSet::ASCII {
assert!(!byte.is_ascii());
}Sourcepub const ASCII_ALPHABETIC: Self
pub const ASCII_ALPHABETIC: Self
The set of all ASCII alphabetic characters:
- U+0041 ‘A’ ..= U+005A ‘Z’
- U+0061 ‘a’ ..= U+007A ‘z’
§Examples
This contains all bytes for which u8::is_ascii_alphabetic returns
true:
for byte in ByteSet::ASCII_ALPHABETIC {
assert!(byte.is_ascii_alphabetic());
}
for byte in !ByteSet::ASCII_ALPHABETIC {
assert!(!byte.is_ascii_alphabetic());
}Sourcepub const ASCII_UPPERCASE: Self
pub const ASCII_UPPERCASE: Self
The set of all ASCII uppercase characters: U+0041 ‘A’ ..= U+005A ‘Z’.
§Examples
This contains all bytes for which u8::is_ascii_uppercase returns
true:
for byte in ByteSet::ASCII_UPPERCASE {
assert!(byte.is_ascii_uppercase());
}
for byte in !ByteSet::ASCII_UPPERCASE {
assert!(!byte.is_ascii_uppercase());
}Sourcepub const ASCII_LOWERCASE: Self
pub const ASCII_LOWERCASE: Self
The set of all ASCII lowercase characters: U+0061 ‘a’ ..= U+007A ‘z’.
§Examples
This contains all bytes for which u8::is_ascii_lowercase returns
true:
for byte in ByteSet::ASCII_LOWERCASE {
assert!(byte.is_ascii_lowercase());
}
for byte in !ByteSet::ASCII_LOWERCASE {
assert!(!byte.is_ascii_lowercase());
}Sourcepub const ASCII_ALPHANUMERIC: Self
pub const ASCII_ALPHANUMERIC: Self
The set of all ASCII alphanumeric characters:
- U+0041 ‘A’ ..= U+005A ‘Z’
- U+0061 ‘a’ ..= U+007A ‘z’
- U+0030 ‘0’ ..= U+0039 ‘9’
§Examples
This contains all bytes for which u8::is_ascii_alphanumeric returns
true:
for byte in ByteSet::ASCII_ALPHANUMERIC {
assert!(byte.is_ascii_alphanumeric());
}
for byte in !ByteSet::ASCII_ALPHANUMERIC {
assert!(!byte.is_ascii_alphanumeric());
}Sourcepub const ASCII_DIGIT: Self
pub const ASCII_DIGIT: Self
The set of all ASCII decimal digits: U+0030 ‘0’ ..= U+0039 ‘9’.
§Examples
This contains all bytes for which u8::is_ascii_digit returns true:
for byte in ByteSet::ASCII_DIGIT {
assert!(byte.is_ascii_digit());
}
for byte in !ByteSet::ASCII_DIGIT {
assert!(!byte.is_ascii_digit());
}Sourcepub const ASCII_HEXDIGIT: Self
pub const ASCII_HEXDIGIT: Self
The set of all ASCII hexadecimal digits:
- U+0030 ‘0’ ..= U+0039 ‘9’
- U+0041 ‘A’ ..= U+0046 ‘F’
- U+0061 ‘a’ ..= U+0066 ‘f’
§Examples
This contains all bytes for which u8::is_ascii_hexdigit returns
true:
for byte in ByteSet::ASCII_HEXDIGIT {
assert!(byte.is_ascii_hexdigit());
}
for byte in !ByteSet::ASCII_HEXDIGIT {
assert!(!byte.is_ascii_hexdigit());
}Sourcepub const ASCII_PUNCTUATION: Self
pub const ASCII_PUNCTUATION: Self
The set of all ASCII punctuation characters:
- U+0021 ..= U+002F
! " # $ % & ' ( ) * + , - . / - U+003A ..= U+0040
: ; < = > ? @ - U+005B ..= U+0060
[ \ ] ^ _ ` - U+007B ..= U+007E
{ | } ~
§Examples
This contains all bytes for which u8::is_ascii_punctuation returns
true:
for byte in ByteSet::ASCII_PUNCTUATION {
assert!(byte.is_ascii_punctuation());
}
for byte in !ByteSet::ASCII_PUNCTUATION {
assert!(!byte.is_ascii_punctuation());
}Sourcepub const ASCII_GRAPHIC: Self
pub const ASCII_GRAPHIC: Self
The set of all ASCII graphic characters: U+0021 ‘!’ ..= U+007E ‘~’.
§Examples
This contains all bytes for which u8::is_ascii_graphic returns
true:
for byte in ByteSet::ASCII_GRAPHIC {
assert!(byte.is_ascii_graphic());
}
for byte in !ByteSet::ASCII_GRAPHIC {
assert!(!byte.is_ascii_graphic());
}Sourcepub const ASCII_WHITESPACE: Self
pub const ASCII_WHITESPACE: Self
The set of all ASCII whitespace characters:
- U+0020 SPACE
- U+0009 HORIZONTAL TAB
- U+000A LINE FEED
- U+000C FORM FEED
- U+000D CARRIAGE RETURN
§Examples
This contains all bytes for which u8::is_ascii_whitespace returns
true:
for byte in ByteSet::ASCII_WHITESPACE {
assert!(byte.is_ascii_whitespace());
}
for byte in !ByteSet::ASCII_WHITESPACE {
assert!(!byte.is_ascii_whitespace());
}Sourcepub const ASCII_CONTROL: Self
pub const ASCII_CONTROL: Self
The set of all ASCII control characters:
- U+0000 NUL ..= U+001F UNIT SEPARATOR
- U+007F DELETE.
Note that most ASCII whitespace characters are control characters, but SPACE is not.
§Examples
This contains all bytes for which u8::is_ascii_control returns
true:
for byte in ByteSet::ASCII_CONTROL {
assert!(byte.is_ascii_control());
}
for byte in !ByteSet::ASCII_CONTROL {
assert!(!byte.is_ascii_control());
}Sourcepub const fn is_ascii(&self) -> bool
pub const fn is_ascii(&self) -> bool
Returns true if u8::is_ascii returns true for all bytes in
self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_alphabetic(&self) -> bool
pub const fn is_ascii_alphabetic(&self) -> bool
Returns true if u8::is_ascii_alphabetic returns true for all
bytes in self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_uppercase(&self) -> bool
pub const fn is_ascii_uppercase(&self) -> bool
Returns true if u8::is_ascii_uppercase returns true for all
bytes in self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_lowercase(&self) -> bool
pub const fn is_ascii_lowercase(&self) -> bool
Returns true if u8::is_ascii_lowercase returns true for all
bytes in self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_alphanumeric(&self) -> bool
pub const fn is_ascii_alphanumeric(&self) -> bool
Returns true if u8::is_ascii_alphanumeric returns true for all
bytes in self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_digit(&self) -> bool
pub const fn is_ascii_digit(&self) -> bool
Returns true if u8::is_ascii_digit returns true for all bytes in
self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_hexdigit(&self) -> bool
pub const fn is_ascii_hexdigit(&self) -> bool
Returns true if u8::is_ascii_hexdigit returns true for all bytes
in self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_punctuation(&self) -> bool
pub const fn is_ascii_punctuation(&self) -> bool
Returns true if u8::is_ascii_punctuation returns true for all
bytes in self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_graphic(&self) -> bool
pub const fn is_ascii_graphic(&self) -> bool
Returns true if u8::is_ascii_graphic returns true for all bytes
in self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_whitespace(&self) -> bool
pub const fn is_ascii_whitespace(&self) -> bool
Returns true if u8::is_ascii_whitespace returns true for all
bytes in self.
This is significantly more efficient than checking each byte in self
individually.
Sourcepub const fn is_ascii_control(&self) -> bool
pub const fn is_ascii_control(&self) -> bool
Returns true if u8::is_ascii_control returns true for all bytes
in self.
This is significantly more efficient than checking each byte in self
individually.
Source§impl ByteSet
Operations over the internal memory representation.
impl ByteSet
Operations over the internal memory representation.
There are currently no stability guarantees over the internal bytes. This is being tracked in #8.
Sourcepub fn as_bytes(&self) -> &[u8; 32]
pub fn as_bytes(&self) -> &[u8; 32]
Returns a shared reference to the underlying bytes of self.
Sourcepub fn as_bytes_mut(&mut self) -> &mut [u8; 32]
pub fn as_bytes_mut(&mut self) -> &mut [u8; 32]
Returns a mutable reference to the underlying bytes of self.
Sourcepub fn slice_as_bytes(slice: &[Self]) -> &[u8] ⓘ
pub fn slice_as_bytes(slice: &[Self]) -> &[u8] ⓘ
Returns a shared reference to the underlying bytes of slice.
Sourcepub fn slice_as_bytes_mut(slice: &mut [Self]) -> &mut [u8] ⓘ
pub fn slice_as_bytes_mut(slice: &mut [Self]) -> &mut [u8] ⓘ
Returns a mutable reference to the underlying bytes of slice.
Trait Implementations§
Source§impl BitAndAssign for ByteSet
impl BitAndAssign for ByteSet
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&= operation. Read moreSource§impl BitOrAssign for ByteSet
impl BitOrAssign for ByteSet
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|= operation. Read moreSource§impl BitXorAssign for ByteSet
impl BitXorAssign for ByteSet
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^= operation. Read moreSource§impl<'de> Deserialize<'de> for ByteSet
Available on crate feature serde only.
impl<'de> Deserialize<'de> for ByteSet
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Distribution<ByteSet> for Standard
Available on crate feature rand only.
impl Distribution<ByteSet> for Standard
rand only.Source§impl<'a> Extend<&'a u8> for ByteSet
impl<'a> Extend<&'a u8> for ByteSet
Source§fn extend<T: IntoIterator<Item = &'a u8>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a u8>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<u8> for ByteSet
impl Extend<u8> for ByteSet
Source§fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl From<RangeInclusive<u8>> for ByteSet
impl From<RangeInclusive<u8>> for ByteSet
Source§fn from(range: RangeInclusive<u8>) -> Self
fn from(range: RangeInclusive<u8>) -> Self
Source§impl From<RangeToInclusive<u8>> for ByteSet
impl From<RangeToInclusive<u8>> for ByteSet
Source§fn from(range: RangeToInclusive<u8>) -> Self
fn from(range: RangeToInclusive<u8>) -> Self
Source§impl<'a> FromIterator<&'a u8> for ByteSet
impl<'a> FromIterator<&'a u8> for ByteSet
Source§impl FromIterator<u8> for ByteSet
impl FromIterator<u8> for ByteSet
Source§impl IntoIterator for ByteSet
impl IntoIterator for ByteSet
Source§impl Ord for ByteSet
impl Ord for ByteSet
Source§impl PartialOrd for ByteSet
impl PartialOrd for ByteSet
Source§impl SubAssign for ByteSet
impl SubAssign for ByteSet
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more