Skip to content

Commit

Permalink
Add Protocol::SUB/SUB_ASSIGN to Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon-bernardo authored and udoprog committed Dec 25, 2024
1 parent c28b33d commit fe2e38b
Showing 1 changed file with 89 additions and 2 deletions.
91 changes: 89 additions & 2 deletions crates/rune-modules/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
m.function_meta(Instant::elapsed__meta)?;
m.function_meta(Instant::add__meta)?;
m.function_meta(Instant::add_assign__meta)?;
m.function_meta(Instant::sub__meta)?;
m.function_meta(Instant::sub_assign__meta)?;
m.function_meta(Instant::sub_instant__meta)?;
m.function_meta(Instant::sub_instant_assign__meta)?;
m.function_meta(Instant::partial_eq__meta)?;
m.implement_trait::<Instant>(item!(::std::cmp::PartialEq))?;
m.function_meta(Instant::eq__meta)?;
Expand Down Expand Up @@ -685,7 +689,7 @@ impl Duration {
VmResult::Ok(Self { inner })
}

/// Add a duration to this instant and return a new instant.
/// Add a duration to this instant.
///
/// # Examples
///
Expand Down Expand Up @@ -1137,7 +1141,7 @@ impl Instant {
VmResult::Ok(Self { inner })
}

/// Add a duration to this instant and return a new instant.
/// Add a duration to this instant.
///
/// # Examples
///
Expand All @@ -1162,6 +1166,89 @@ impl Instant {
VmResult::Ok(())
}

/// Subtract a duration and return a new Instant.
///
/// # Examples
///
/// ```rune
/// use time::{Duration, Instant};
///
/// let first = Instant::now();
/// let second = first - Duration::SECOND;
/// ```
#[rune::function(keep, instance, protocol = SUB)]
#[inline]
fn sub(&self, duration: &Duration) -> VmResult<Self> {
let Some(inner) = self.inner.checked_sub(duration.inner) else {
vm_panic!("overflow when subtract duration from instant")
};

VmResult::Ok(Self { inner })
}

/// Subtract a duration from this instant.
///
/// # Examples
///
/// ```rune
/// use time::{Duration, Instant};
///
/// let first = Instant::now();
/// first -= Duration::SECOND;
/// ```
#[rune::function(keep, instance, protocol = SUB_ASSIGN)]
#[inline]
fn sub_assign(&mut self, duration: &Duration) -> VmResult<()> {
let Some(inner) = self.inner.checked_sub(duration.inner) else {
vm_panic!("overflow when subtract duration from instant")
};

self.inner = inner;
VmResult::Ok(())
}

/// Subtract a instant and return a new Duration.
///
/// # Examples
///
/// ```rune
/// use time::{Duration, Instant};
///
/// let first = Instant::now();
/// let duration = first - Instant::now();
/// ```
#[rune::function(keep, instance, protocol = SUB)]
#[inline]
fn sub_instant(&self, instant: &Instant) -> VmResult<Duration> {
let Some(inner) = self.inner.checked_duration_since(instant.inner) else {
vm_panic!("overflow when subtract instant")
};

VmResult::Ok(Duration::from_std(inner))
}

/// Subtract a instant from this instant.
///
/// # Examples
///
/// ```rune
/// use std::ops::partial_eq;
/// use time::{Duration, Instant};
///
/// let first = Instant::now();
/// first -= Instant::now();
/// ```
#[rune::function(keep, instance, protocol = SUB_ASSIGN)]
#[inline]
fn sub_instant_assign(&mut self, instant: &Instant) -> VmResult<()> {
let Some(inner) = self.inner.checked_duration_since(instant.inner) else {
vm_panic!("overflow when subtract instant")
};

self.inner -= inner;
VmResult::Ok(())
}

/// Test two instants for partial equality.
///
/// # Examples
Expand Down

0 comments on commit fe2e38b

Please sign in to comment.