Trait AtomSet

Source
pub trait AtomSet: Default + Debug {
    // Required methods
    fn from_str(keyword: &str) -> Self;
    fn to_str(self) -> &'static str;
    fn len(&self) -> u32;
    fn from_bits(bits: u32) -> Self;
    fn as_bits(&self) -> u32;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

§Usage with #[derive(AtomSet)]

The easiest way to implement this trait is using the AtomSet derive macro:

use derive_atom_set::AtomSet;
use css_lexer::AtomSet;

#[derive(Debug, Default, Copy, Clone, PartialEq, AtomSet)]
pub enum Units {
  // AtomSet must derive default, ideally with an empty atom, or equivalent!
  #[default]
  _None,

  // Automatically converts to "px"
  Px,

  // Automatically converts to "rem"
  Rem,

  // Custom string mapping
  #[atom("%")]
  Percent,
}

Required Methods§

Source

fn from_str(keyword: &str) -> Self

Converts a string keyword to the corresponding atom variant.

This performs case-insensitive matching and returns the Empty variant for unrecognized strings.

§Examples
use derive_atom_set::*;

#[derive(Debug, Default, Copy, Clone, PartialEq, AtomSet)]
enum MyAtomSet {
  #[default]
  _None,
  Url
}
assert_eq!(MyAtomSet::from_str("url"), MyAtomSet::Url);
assert_eq!(MyAtomSet::from_str("URL"), MyAtomSet::Url);  // Case insensitive
assert_eq!(MyAtomSet::from_str("unknown"), MyAtomSet::_None);
Source

fn to_str(self) -> &'static str

Converts this atom back to its string representation.

Returns a static string slice that represents this atom’s canonical form.

The variant marked #[default] will always return the empty string.

§Examples
use derive_atom_set::*;

#[derive(Debug, Default, Copy, Clone, PartialEq, AtomSet)]
enum MyAtomSet {
  #[default]
  _None,
  Url
}
assert_eq!(MyAtomSet::Url.to_str(), "url");
assert_eq!(MyAtomSet::_None.to_str(), "");

// Round-trip conversion
let atom = MyAtomSet::from_str("url");
assert_eq!(atom.to_str(), "url");
Source

fn len(&self) -> u32

Returns the length in characters of this atom’s string representation.

This is equivalent to self.to_str().len() but may be more efficient depending on the implementation.

§Examples
use derive_atom_set::*;

#[derive(Debug, Default, Copy, Clone, PartialEq, AtomSet)]
enum MyAtomSet {
  #[default]
  _None,
  Url
}
assert_eq!(MyAtomSet::Url.len(), 3);
assert_eq!(MyAtomSet::_None.len(), 0);
Source

fn from_bits(bits: u32) -> Self

Converts a numeric bit representation back to an atom variant.

This is used internally for efficient storage and retrieval. Returns the Empty variant for unrecognized bit values.

§Examples
use derive_atom_set::*;

#[derive(Debug, Default, Copy, Clone, PartialEq, AtomSet)]
enum MyAtomSet {
  #[default]
  _None,
  Url
}
let atom = MyAtomSet::Url;
let bits = atom.as_bits();
let restored = MyAtomSet::from_bits(bits);
assert_eq!(atom, restored);
Source

fn as_bits(&self) -> u32

Converts this atom to its numeric bit representation.

This is used internally for efficient storage. The bit value corresponds to the enum discriminant.

§Examples
use derive_atom_set::*;

#[derive(Debug, Default, Copy, Clone, PartialEq, AtomSet)]
enum MyAtomSet {
  #[default]
  _None,
  Url
}
let bits = MyAtomSet::Url.as_bits();
assert_eq!(MyAtomSet::from_bits(bits), MyAtomSet::Url);

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the length of this atom is 0.

This is equivalent to self.to_str().is_empty() but may be more efficient depending on the implementation.

§Examples
use derive_atom_set::*;

#[derive(Debug, Default, Copy, Clone, PartialEq, AtomSet)]
enum MyAtomSet {
  #[default]
  _None,
  Url
}
assert!(!MyAtomSet::Url.is_empty());
assert!(MyAtomSet::_None.is_empty());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§