@@ -199,6 +199,10 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
199
199
m. function_meta ( Instant :: elapsed__meta) ?;
200
200
m. function_meta ( Instant :: add__meta) ?;
201
201
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) ?;
202
206
m. function_meta ( Instant :: partial_eq__meta) ?;
203
207
m. implement_trait :: < Instant > ( item ! ( :: std:: cmp:: PartialEq ) ) ?;
204
208
m. function_meta ( Instant :: eq__meta) ?;
@@ -685,7 +689,7 @@ impl Duration {
685
689
VmResult :: Ok ( Self { inner } )
686
690
}
687
691
688
- /// Add a duration to this instant and return a new instant .
692
+ /// Add a duration to this instant.
689
693
///
690
694
/// # Examples
691
695
///
@@ -1137,7 +1141,7 @@ impl Instant {
1137
1141
VmResult :: Ok ( Self { inner } )
1138
1142
}
1139
1143
1140
- /// Add a duration to this instant and return a new instant .
1144
+ /// Add a duration to this instant.
1141
1145
///
1142
1146
/// # Examples
1143
1147
///
@@ -1162,6 +1166,89 @@ impl Instant {
1162
1166
VmResult :: Ok ( ( ) )
1163
1167
}
1164
1168
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
+
1165
1252
/// Test two instants for partial equality.
1166
1253
///
1167
1254
/// # Examples
0 commit comments