Skip to content

Instantly share code, notes, and snippets.

@Ygg01
Created January 14, 2015 06:39
Show Gist options
  • Save Ygg01/6d1bd08b8e12460da9fc to your computer and use it in GitHub Desktop.
Save Ygg01/6d1bd08b8e12460da9fc to your computer and use it in GitHub Desktop.
Example of a Calendar using associative types
struct Instant {
nanos: i64,
}
trait ChronoUnit {
fn convert_to_nanos(&self) -> i64;
fn is_estimate(&self) -> bool;
// get_estimate()
}
trait Calendar {
type Sec : ChronoUnit;
type Min : ChronoUnit;
type Hrs : ChronoUnit;
type Day : ChronoUnit;
type Mon : ChronoUnit;
}
struct Second;
struct Minute;
struct Hour;
struct Day;
struct Month;
impl ChronoUnit for Second {
fn convert_to_nanos(&self) -> i64 {
1_000_000
}
fn is_estimate(&self) -> bool {
false
}
}
impl ChronoUnit for Minute {
fn convert_to_nanos(&self) -> i64 {
60_000_000
}
fn is_estimate(&self) -> bool {
false
}
}
impl ChronoUnit for Hour {
fn convert_to_nanos(&self) -> i64 {
3_600_000_000
}
fn is_estimate(&self) -> bool {
false
}
}
impl ChronoUnit for Day {
fn convert_to_nanos(&self) -> i64 {
86_400_000_000
}
fn is_estimate(&self) -> bool {
false
}
}
impl ChronoUnit for Month {
fn convert_to_nanos(&self) -> i64 {
0
}
fn is_estimate(&self) -> bool {
true
}
}
struct GregorianCalendar{
inst: Instant
}
impl Calendar for GregorianCalendar {
type Sec = Second;
type Min = Minute;
type Hrs = Hour;
type Day = Day;
type Mon = Month;
}// Add code here
fn main() {
// Add code here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment