Struct WeekdaySet

pub struct WeekdaySet(/* private field */);

A collection of Weekdays stored as a single byte.

This type is Copy and provides efficient set-like and slice-like operations. Many operations are const as well.

Implemented as a bitmask where bits 1-7 correspond to Monday-Sunday.

Implementations

impl WeekdaySet

const fn from_array<const C: usize>(days: [Weekday; C]) -> Self

Create a WeekdaySet from an array of Weekdays.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::EMPTY, WeekdaySet::from_array([]));
assert_eq!(WeekdaySet::single(Mon), WeekdaySet::from_array([Mon]));
assert_eq!(WeekdaySet::ALL, WeekdaySet::from_array([Mon, Tue, Wed, Thu, Fri, Sat, Sun]));
const fn single(weekday: Weekday) -> Self

Create a WeekdaySet from a single Weekday.

const fn single_day(self) -> Option<Weekday>

Returns Some(day) if this collection contains exactly one day.

Returns None otherwise.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::single(Mon).single_day(), Some(Mon));
assert_eq!(WeekdaySet::from_array([Mon, Tue]).single_day(), None);
assert_eq!(WeekdaySet::EMPTY.single_day(), None);
assert_eq!(WeekdaySet::ALL.single_day(), None);
fn insert(&mut self, day: Weekday) -> bool

Adds a day to the collection.

Returns true if the day was new to the collection.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
let mut weekdays = WeekdaySet::single(Mon);
assert!(weekdays.insert(Tue));
assert!(!weekdays.insert(Tue));
fn remove(&mut self, day: Weekday) -> bool

Removes a day from the collection.

Returns true if the collection did contain the day.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
let mut weekdays = WeekdaySet::single(Mon);
assert!(weekdays.remove(Mon));
assert!(!weekdays.remove(Mon));
const fn is_subset(self, other: Self) -> bool

Returns true if other contains all days in self.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert!(WeekdaySet::single(Mon).is_subset(WeekdaySet::ALL));
assert!(!WeekdaySet::single(Mon).is_subset(WeekdaySet::EMPTY));
assert!(WeekdaySet::EMPTY.is_subset(WeekdaySet::single(Mon)));
const fn intersection(self, other: Self) -> Self

Returns days that are in both self and other.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::single(Mon).intersection(WeekdaySet::single(Mon)), WeekdaySet::single(Mon));
assert_eq!(WeekdaySet::single(Mon).intersection(WeekdaySet::single(Tue)), WeekdaySet::EMPTY);
assert_eq!(WeekdaySet::ALL.intersection(WeekdaySet::single(Mon)), WeekdaySet::single(Mon));
assert_eq!(WeekdaySet::ALL.intersection(WeekdaySet::EMPTY), WeekdaySet::EMPTY);
const fn union(self, other: Self) -> Self

Returns days that are in either self or other.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::single(Mon).union(WeekdaySet::single(Mon)), WeekdaySet::single(Mon));
assert_eq!(WeekdaySet::single(Mon).union(WeekdaySet::single(Tue)), WeekdaySet::from_array([Mon, Tue]));
assert_eq!(WeekdaySet::ALL.union(WeekdaySet::single(Mon)), WeekdaySet::ALL);
assert_eq!(WeekdaySet::ALL.union(WeekdaySet::EMPTY), WeekdaySet::ALL);
const fn symmetric_difference(self, other: Self) -> Self

Returns days that are in self or other but not in both.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::single(Mon).symmetric_difference(WeekdaySet::single(Mon)), WeekdaySet::EMPTY);
assert_eq!(WeekdaySet::single(Mon).symmetric_difference(WeekdaySet::single(Tue)), WeekdaySet::from_array([Mon, Tue]));
assert_eq!(
    WeekdaySet::ALL.symmetric_difference(WeekdaySet::single(Mon)),
    WeekdaySet::from_array([Tue, Wed, Thu, Fri, Sat, Sun]),
);
assert_eq!(WeekdaySet::ALL.symmetric_difference(WeekdaySet::EMPTY), WeekdaySet::ALL);
const fn difference(self, other: Self) -> Self

Returns days that are in self but not in other.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::single(Mon).difference(WeekdaySet::single(Mon)), WeekdaySet::EMPTY);
assert_eq!(WeekdaySet::single(Mon).difference(WeekdaySet::single(Tue)), WeekdaySet::single(Mon));
assert_eq!(WeekdaySet::EMPTY.difference(WeekdaySet::single(Mon)), WeekdaySet::EMPTY);
const fn first(self) -> Option<Weekday>

Get the first day in the collection, starting from Monday.

Returns None if the collection is empty.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::single(Mon).first(), Some(Mon));
assert_eq!(WeekdaySet::single(Tue).first(), Some(Tue));
assert_eq!(WeekdaySet::ALL.first(), Some(Mon));
assert_eq!(WeekdaySet::EMPTY.first(), None);
fn last(self) -> Option<Weekday>

Get the last day in the collection, starting from Sunday.

Returns None if the collection is empty.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::single(Mon).last(), Some(Mon));
assert_eq!(WeekdaySet::single(Sun).last(), Some(Sun));
assert_eq!(WeekdaySet::from_array([Mon, Tue]).last(), Some(Tue));
assert_eq!(WeekdaySet::EMPTY.last(), None);
const fn iter(self, start: Weekday) -> WeekdaySetIter

Iterate over the Weekdays in the collection starting from a given day.

Wraps around from Sunday to Monday if necessary.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
let weekdays = WeekdaySet::from_array([Mon, Wed, Fri]);
let mut iter = weekdays.iter(Wed);
assert_eq!(iter.next(), Some(Wed));
assert_eq!(iter.next(), Some(Fri));
assert_eq!(iter.next(), Some(Mon));
assert_eq!(iter.next(), None);
const fn contains(self, day: Weekday) -> bool

Returns true if the collection contains the given day.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert!(WeekdaySet::single(Mon).contains(Mon));
assert!(WeekdaySet::from_array([Mon, Tue]).contains(Tue));
assert!(!WeekdaySet::single(Mon).contains(Tue));
const fn is_empty(self) -> bool

Returns true if the collection is empty.

Example

# use chrono::{Weekday, WeekdaySet};
assert!(WeekdaySet::EMPTY.is_empty());
assert!(!WeekdaySet::single(Weekday::Mon).is_empty());
const fn len(self) -> u8

Returns the number of days in the collection.

Example

# use chrono::WeekdaySet;
use chrono::Weekday::*;
assert_eq!(WeekdaySet::single(Mon).len(), 1);
assert_eq!(WeekdaySet::from_array([Mon, Wed, Fri]).len(), 3);
assert_eq!(WeekdaySet::ALL.len(), 7);
const EMPTY: Self = _;

An empty WeekdaySet.

const ALL: Self = _;

A WeekdaySet containing all seven Weekdays.

Trait Implementations

impl Clone for WeekdaySet

fn clone(&self) -> WeekdaySet

impl Copy for WeekdaySet

impl Debug for WeekdaySet

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Default for WeekdaySet

fn default() -> WeekdaySet

impl Display for WeekdaySet

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Eq for WeekdaySet

impl FromIterator<Weekday> for WeekdaySet

fn from_iter<T: IntoIterator<Item = Weekday>>(iter: T) -> Self

impl Hash for WeekdaySet

fn hash<__H: Hasher>(&self, state: &mut __H)

impl Ord for WeekdaySet

fn cmp(&self, other: &WeekdaySet) -> Ordering

impl PartialEq for WeekdaySet

fn eq(&self, other: &WeekdaySet) -> bool

impl PartialOrd for WeekdaySet

fn partial_cmp(&self, other: &WeekdaySet) -> Option<Ordering>

impl StructuralPartialEq for WeekdaySet

Auto Trait Implementations

impl Freeze for WeekdaySet

impl RefUnwindSafe for WeekdaySet

impl Send for WeekdaySet

impl Sync for WeekdaySet

impl Unpin for WeekdaySet

impl UnsafeUnpin for WeekdaySet

impl UnwindSafe for WeekdaySet

Blanket Implementations

impl<T> Any for WeekdaySet where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for WeekdaySet where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for WeekdaySet where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for WeekdaySet where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for WeekdaySet

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for WeekdaySet where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> ToString for WeekdaySet where T: Display + ?Sized,

fn to_string(&self) -> String

impl<T, U> Into<U> for WeekdaySet where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for WeekdaySet where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for WeekdaySet where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>