CompactCalendar

Struct CompactCalendar 

Source
pub struct CompactCalendar { /* private fields */ }
Expand description

A compact representation of included days in a range of years, using u32-based bit arrays.

Implementations§

Source§

impl CompactCalendar

Source

pub fn year_for(&self, date: NaiveDate) -> Option<&CompactYear>

Get a reference to the year containing give date.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
let day2 = NaiveDate::from_ymd_opt(2055, 3, 5).unwrap();

let mut cal = CompactCalendar::default();
cal.insert(day1);

assert!(cal.year_for(day1).unwrap().contains(11, 3));
assert!(cal.year_for(day2).is_none());
Source

pub fn year_for_mut(&mut self, date: NaiveDate) -> Option<&mut CompactYear>

Get a mutable reference to the year containing give date.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
let day3 = NaiveDate::from_ymd_opt(2055, 7, 5).unwrap();

let mut cal = CompactCalendar::default();
assert!(cal.year_for_mut(day3).is_none());

cal.insert(day1);
assert!(cal.year_for_mut(day1).unwrap().contains(11, 3));
Source

pub fn insert(&mut self, date: NaiveDate) -> bool

Include a day in this calendar. Return false if the day already belonged to the calendar.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
let day3 = NaiveDate::from_ymd_opt(2055, 9, 7).unwrap();

let mut cal = CompactCalendar::default();
assert!(cal.insert(day1));
assert!(cal.insert(day2));
assert!(cal.insert(day3));
assert!(!cal.insert(day1));
assert_eq!(cal.count(), 3);
Source

pub fn contains(&self, date: NaiveDate) -> bool

Check if this calendar includes the given day.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
let day3 = NaiveDate::from_ymd_opt(2022, 8, 12).unwrap();

let mut cal = CompactCalendar::default();
cal.insert(day1);
cal.insert(day2);

assert!(cal.contains(day1));
assert!(cal.contains(day2));
assert!(!cal.contains(day3));
Source

pub fn iter(&self) -> impl Iterator<Item = NaiveDate> + Send + Sync + '_

Iterate over the days included in this calendar.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
let day3 = NaiveDate::from_ymd_opt(2022, 8, 12).unwrap();

let mut cal = CompactCalendar::default();
cal.insert(day3);
cal.insert(day1);
cal.insert(day2);

let days: Vec<_> = cal.iter().collect();
assert_eq!(days, [day1, day2, day3])
Source

pub fn first_after(&self, date: NaiveDate) -> Option<NaiveDate>

Get the first day included in this calendar that follows the input day, if such a day exists.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let day0 = NaiveDate::from_ymd_opt(2010, 1, 1).unwrap();
let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
let day3 = NaiveDate::from_ymd_opt(2022, 8, 12).unwrap();

let mut cal = CompactCalendar::default();
cal.insert(day1);
cal.insert(day2);
cal.insert(day3);

assert_eq!(cal.first_after(day0), Some(day1));
assert_eq!(cal.first_after(day2), Some(day3));
assert_eq!(cal.first_after(day3), None);
Source

pub fn count(&self) -> u32

Count number of days included for this calendar.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let mut cal = CompactCalendar::default();
cal.insert(NaiveDate::from_ymd_opt(2022, 8, 12).unwrap());
cal.insert(NaiveDate::from_ymd_opt(2022, 3, 5).unwrap());
assert_eq!(cal.count(), 2);
Source

pub fn serialize(&self, writer: impl Write) -> Result<()>

Serialize this calendar into a writer.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let mut cal = CompactCalendar::default();

let mut buf1 = Vec::new();
cal.insert(NaiveDate::from_ymd_opt(2022, 8, 12).unwrap());
cal.serialize(&mut buf1).unwrap();

let mut buf2 = Vec::new();
cal.insert(NaiveDate::from_ymd_opt(2022, 3, 5).unwrap());
cal.serialize(&mut buf2).unwrap();

assert_ne!(buf1, buf2);
Source

pub fn deserialize(reader: impl Read) -> Result<Self>

Deserialize a calendar from a reader.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let mut cal1 = CompactCalendar::default();
cal1.insert(NaiveDate::from_ymd_opt(2022, 8, 12).unwrap());
cal1.insert(NaiveDate::from_ymd_opt(2022, 3, 5).unwrap());

let mut buf = Vec::new();
cal1.serialize(&mut buf).unwrap();

let cal2 = CompactCalendar::deserialize(buf.as_slice()).unwrap();
assert_eq!(cal1, cal2);

Trait Implementations§

Source§

impl Clone for CompactCalendar

Source§

fn clone(&self) -> CompactCalendar

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CompactCalendar

Source§

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

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let mut cal = CompactCalendar::default();
cal.insert(NaiveDate::from_ymd_opt(2022, 8, 12).unwrap());
cal.insert(NaiveDate::from_ymd_opt(2022, 3, 5).unwrap());
assert_eq!(format!("{cal:?}"), "CompactCalendar({2022-03-05, 2022-08-12})");
Source§

impl Default for CompactCalendar

Source§

fn default() -> Self

Create a new year that does not include any day.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let mut cal = CompactCalendar::default();
assert_eq!(cal.count(), 0);
Source§

impl FromIterator<NaiveDate> for CompactCalendar

Source§

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

Create a calendar from a list of dates.

use compact_calendar::CompactCalendar;
use chrono::NaiveDate;

let dates = [
    NaiveDate::from_ymd_opt(2013, 11, 3).unwrap(),
    NaiveDate::from_ymd_opt(2022, 3, 5).unwrap(),
    NaiveDate::from_ymd_opt(2055, 7, 5).unwrap(),
    NaiveDate::from_ymd_opt(2013, 11, 3).unwrap(),
];

let cal: CompactCalendar = dates.iter().copied().collect();
assert_eq!(cal.count(), 3);
assert!(cal.contains(dates[0]));
Source§

impl Hash for CompactCalendar

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for CompactCalendar

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for CompactCalendar

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for CompactCalendar

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for CompactCalendar

Source§

impl StructuralPartialEq for CompactCalendar

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.