Struct compact_calendar::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
sourceimpl CompactCalendar
impl CompactCalendar
sourcepub fn new(first_year: i32, last_year: i32) -> Self
pub fn new(first_year: i32, last_year: i32) -> Self
Create a new year that does not include any day but has the capacity to add any new day in
the range of [first_year, last_year].
use compact_calendar::CompactCalendar;
use chrono::NaiveDate;
let day1 = NaiveDate::from_ymd(2013, 11, 3);
let day2 = NaiveDate::from_ymd(2022, 3, 5);
let day3 = NaiveDate::from_ymd(2055, 7, 5);
let mut cal = CompactCalendar::new(2020, 2050);
assert!(!cal.insert(day1));
assert!(cal.insert(day2));
assert!(!cal.insert(day3));
assert_eq!(cal.count(), 1);sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Create a new calendar year that does not include any day and does not have the capacity to add any.
use compact_calendar::CompactCalendar;
use chrono::NaiveDate;
let mut cal = CompactCalendar::empty();
assert!(!cal.insert(NaiveDate::from_ymd(2013, 11, 3)));
assert_eq!(cal.count(), 0);sourcepub fn year_for(&self, date: NaiveDate) -> Option<&CompactYear>
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(2013, 11, 3);
let day2 = NaiveDate::from_ymd(2055, 3, 5);
let mut cal = CompactCalendar::new(2000, 2050);
cal.insert(day1);
assert!(cal.year_for(day1).unwrap().contains(11, 3));
assert!(cal.year_for(day2).is_none());sourcepub fn year_for_mut(&mut self, date: NaiveDate) -> Option<&mut CompactYear>
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(2013, 11, 3);
let day2 = NaiveDate::from_ymd(2022, 3, 5);
let day3 = NaiveDate::from_ymd(2055, 7, 5);
let mut cal = CompactCalendar::new(2000, 2050);
assert!(cal.year_for_mut(day3).is_none());
cal.insert(day1);
assert!(cal.year_for_mut(day1).unwrap().contains(11, 3));
cal.year_for_mut(day2).unwrap().insert(3, 5);
assert!(cal.contains(day2));sourcepub fn insert(&mut self, date: NaiveDate) -> bool
pub fn insert(&mut self, date: NaiveDate) -> bool
Include a day in this calendar. Return false if the given day was not inserted because it does not belong to the year range of this calendar.
use compact_calendar::CompactCalendar;
use chrono::NaiveDate;
let day1 = NaiveDate::from_ymd(2013, 11, 3);
let day2 = NaiveDate::from_ymd(2022, 3, 5);
let day3 = NaiveDate::from_ymd(2055, 9, 7);
let mut cal = CompactCalendar::new(2000, 2050);
assert!(cal.insert(day1));
assert!(cal.insert(day1));
assert!(cal.insert(day2));
assert_eq!(cal.count(), 2);
assert!(!cal.insert(day3));
assert_eq!(cal.count(), 2);sourcepub fn contains(&self, date: NaiveDate) -> bool
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(2013, 11, 3);
let day2 = NaiveDate::from_ymd(2022, 3, 5);
let day3 = NaiveDate::from_ymd(2022, 8, 12);
let mut cal = CompactCalendar::new(2000, 2050);
cal.insert(day1);
cal.insert(day2);
assert!(cal.contains(day1));
assert!(cal.contains(day2));
assert!(!cal.contains(day3));sourcepub fn iter(&self) -> impl Iterator<Item = NaiveDate> + '_
pub fn iter(&self) -> impl Iterator<Item = NaiveDate> + '_
Iterate over the days included in this calendar.
use compact_calendar::CompactCalendar;
use chrono::NaiveDate;
let day1 = NaiveDate::from_ymd(2013, 11, 3);
let day2 = NaiveDate::from_ymd(2022, 3, 5);
let day3 = NaiveDate::from_ymd(2022, 8, 12);
let mut cal = CompactCalendar::new(2000, 2050);
cal.insert(day3);
cal.insert(day1);
cal.insert(day2);
let days: Vec<_> = cal.iter().collect();
assert_eq!(days, [day1, day2, day3])sourcepub fn first_after(&self, date: NaiveDate) -> Option<NaiveDate>
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(2010, 1, 1);
let day1 = NaiveDate::from_ymd(2013, 11, 3);
let day2 = NaiveDate::from_ymd(2022, 3, 5);
let day3 = NaiveDate::from_ymd(2022, 8, 12);
let mut cal = CompactCalendar::new(2000, 2050);
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);sourcepub fn count(&self) -> u32
pub fn count(&self) -> u32
Count number of days included for this calendar.
use compact_calendar::CompactCalendar;
use chrono::NaiveDate;
let mut cal = CompactCalendar::new(2000, 2050);
cal.insert(NaiveDate::from_ymd(2022, 8, 12));
cal.insert(NaiveDate::from_ymd(2022, 3, 5));
assert_eq!(cal.count(), 2);sourcepub fn serialize(&self, writer: impl Write) -> Result<()>
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::new(2000, 2050);
let mut buf1 = Vec::new();
cal.insert(NaiveDate::from_ymd(2022, 8, 12));
cal.serialize(&mut buf1).unwrap();
let mut buf2 = Vec::new();
cal.insert(NaiveDate::from_ymd(2022, 3, 5));
cal.serialize(&mut buf2).unwrap();
assert_ne!(buf1, buf2);sourcepub fn deserialize(reader: impl Read) -> Result<Self>
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::new(2000, 2050);
cal1.insert(NaiveDate::from_ymd(2022, 8, 12));
cal1.insert(NaiveDate::from_ymd(2022, 3, 5));
let mut buf = Vec::new();
cal1.serialize(&mut buf).unwrap();
let cal2 = CompactCalendar::deserialize(buf.as_slice()).unwrap();
assert_eq!(cal1, cal2);Trait Implementations
sourceimpl Clone for CompactCalendar
impl Clone for CompactCalendar
sourcefn clone(&self) -> CompactCalendar
fn clone(&self) -> CompactCalendar
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source. Read more
sourceimpl Debug for CompactCalendar
impl Debug for CompactCalendar
sourcefn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
use compact_calendar::CompactCalendar;
use chrono::NaiveDate;
let mut cal = CompactCalendar::new(2000, 2050);
cal.insert(NaiveDate::from_ymd(2022, 8, 12));
cal.insert(NaiveDate::from_ymd(2022, 3, 5));
assert_eq!(
format!("{cal:?}"),
"CompactCalendar { first_year: 2000, last_year: 2050, calendar: {2022-03-05, 2022-08-12} }",
);sourceimpl Hash for CompactCalendar
impl Hash for CompactCalendar
sourceimpl Ord for CompactCalendar
impl Ord for CompactCalendar
sourcefn cmp(&self, other: &CompactCalendar) -> Ordering
fn cmp(&self, other: &CompactCalendar) -> Ordering
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl PartialEq<CompactCalendar> for CompactCalendar
impl PartialEq<CompactCalendar> for CompactCalendar
sourcefn eq(&self, other: &CompactCalendar) -> bool
fn eq(&self, other: &CompactCalendar) -> bool
This method tests for self and other values to be equal, and is used
by ==. Read more
sourcefn ne(&self, other: &CompactCalendar) -> bool
fn ne(&self, other: &CompactCalendar) -> bool
This method tests for !=.
sourceimpl PartialOrd<CompactCalendar> for CompactCalendar
impl PartialOrd<CompactCalendar> for CompactCalendar
sourcefn partial_cmp(&self, other: &CompactCalendar) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
impl Eq for CompactCalendar
impl StructuralEq for CompactCalendar
impl StructuralPartialEq for CompactCalendar
Auto Trait Implementations
impl RefUnwindSafe for CompactCalendar
impl Send for CompactCalendar
impl Sync for CompactCalendar
impl Unpin for CompactCalendar
impl UnwindSafe for CompactCalendar
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more