Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Protocol::SUB/SUB_ASSIGN to Instant #888

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading