Skip to content

Commit 5c1a16c

Browse files
Add Protocol::SUB/SUB_ASSIGN to Instant
1 parent 496577c commit 5c1a16c

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

crates/rune-modules/src/time.rs

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
199199
m.function_meta(Instant::elapsed__meta)?;
200200
m.function_meta(Instant::add__meta)?;
201201
m.function_meta(Instant::add_assign__meta)?;
202+
m.function_meta(Instant::sub__meta)?;
203+
m.function_meta(Instant::sub_assign__meta)?;
204+
m.function_meta(Instant::sub_instant__meta)?;
205+
m.function_meta(Instant::sub_instant_assign__meta)?;
202206
m.function_meta(Instant::partial_eq__meta)?;
203207
m.implement_trait::<Instant>(item!(::std::cmp::PartialEq))?;
204208
m.function_meta(Instant::eq__meta)?;
@@ -685,7 +689,7 @@ impl Duration {
685689
VmResult::Ok(Self { inner })
686690
}
687691

688-
/// Add a duration to this instant and return a new instant.
692+
/// Add a duration to this instant.
689693
///
690694
/// # Examples
691695
///
@@ -1137,7 +1141,7 @@ impl Instant {
11371141
VmResult::Ok(Self { inner })
11381142
}
11391143

1140-
/// Add a duration to this instant and return a new instant.
1144+
/// Add a duration to this instant.
11411145
///
11421146
/// # Examples
11431147
///
@@ -1162,6 +1166,89 @@ impl Instant {
11621166
VmResult::Ok(())
11631167
}
11641168

1169+
/// Subtract a duration and return a new Instant.
1170+
///
1171+
/// # Examples
1172+
///
1173+
/// ```rune
1174+
/// use time::{Duration, Instant};
1175+
///
1176+
/// let first = Instant::now();
1177+
/// let second = first - Duration::SECOND;
1178+
/// ```
1179+
#[rune::function(keep, instance, protocol = SUB)]
1180+
#[inline]
1181+
fn sub(&self, duration: &Duration) -> VmResult<Self> {
1182+
let Some(inner) = self.inner.checked_sub(duration.inner) else {
1183+
vm_panic!("overflow when subtract duration from instant")
1184+
};
1185+
1186+
VmResult::Ok(Self { inner })
1187+
}
1188+
1189+
/// Subtract a duration from this instant.
1190+
///
1191+
/// # Examples
1192+
///
1193+
/// ```rune
1194+
/// use time::{Duration, Instant};
1195+
///
1196+
/// let first = Instant::now();
1197+
/// first -= Duration::SECOND;
1198+
/// ```
1199+
#[rune::function(keep, instance, protocol = SUB_ASSIGN)]
1200+
#[inline]
1201+
fn sub_assign(&mut self, duration: &Duration) -> VmResult<()> {
1202+
let Some(inner) = self.inner.checked_sub(duration.inner) else {
1203+
vm_panic!("overflow when subtract duration from instant")
1204+
};
1205+
1206+
self.inner = inner;
1207+
VmResult::Ok(())
1208+
}
1209+
1210+
/// Subtract a instant and return a new Duration.
1211+
///
1212+
/// # Examples
1213+
///
1214+
/// ```rune
1215+
/// use time::{Duration, Instant};
1216+
///
1217+
/// let first = Instant::now();
1218+
/// let duration = first - Instant::now();
1219+
/// ```
1220+
#[rune::function(keep, instance, protocol = SUB)]
1221+
#[inline]
1222+
fn sub_instant(&self, instant: &Instant) -> VmResult<Duration> {
1223+
let Some(inner) = self.inner.checked_duration_since(instant.inner) else {
1224+
vm_panic!("overflow when subtract instant")
1225+
};
1226+
1227+
VmResult::Ok(Duration::from_std(inner))
1228+
}
1229+
1230+
/// Subtract a instant from this instant.
1231+
///
1232+
/// # Examples
1233+
///
1234+
/// ```rune
1235+
/// use std::ops::partial_eq;
1236+
/// use time::{Duration, Instant};
1237+
///
1238+
/// let first = Instant::now();
1239+
/// first -= Instant::now();
1240+
/// ```
1241+
#[rune::function(keep, instance, protocol = SUB_ASSIGN)]
1242+
#[inline]
1243+
fn sub_instant_assign(&mut self, instant: &Instant) -> VmResult<()> {
1244+
let Some(inner) = self.inner.checked_duration_since(instant.inner) else {
1245+
vm_panic!("overflow when subtract instant")
1246+
};
1247+
1248+
self.inner -= inner;
1249+
VmResult::Ok(())
1250+
}
1251+
11651252
/// Test two instants for partial equality.
11661253
///
11671254
/// # Examples

0 commit comments

Comments
 (0)