Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Web Instant
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
Web Instant
Commits
bd3445c2
Verified
Commit
bd3445c2
authored
2 years ago
by
Louis
Browse files
Options
Downloads
Patches
Plain Diff
Implement full Instant interface
parent
6e030925
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
Cargo.toml
+1
-1
1 addition, 1 deletion
Cargo.toml
src/instant_desktop.rs
+75
-10
75 additions, 10 deletions
src/instant_desktop.rs
src/instant_web.rs
+88
-14
88 additions, 14 deletions
src/instant_web.rs
src/lib.rs
+2
-2
2 additions, 2 deletions
src/lib.rs
with
167 additions
and
27 deletions
.gitignore
+
1
−
0
View file @
bd3445c2
/target
/target
/Cargo.lock
/Cargo.lock
.idea/
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Cargo.toml
+
1
−
1
View file @
bd3445c2
[package]
[package]
name
=
"web_instant"
name
=
"web_instant"
version
=
"0.
1
.0"
version
=
"0.
2
.0"
edition
=
"2021"
edition
=
"2021"
description
=
"Cross platform impl of Instant"
description
=
"Cross platform impl of Instant"
authors
=
[
authors
=
[
...
...
This diff is collapsed.
Click to expand it.
src/instant_desktop.rs
+
75
−
10
View file @
bd3445c2
use
std
::
ops
::
Sub
;
use
std
::
fmt
::{
Debug
,
Formatter
};
use
std
::
ops
::{
Add
,
AddAssign
,
Sub
,
SubAssign
};
use
std
::
time
::{
Duration
,
Instant
};
use
std
::
time
::{
Duration
,
Instant
};
#[derive(Copy,
Clone,
PartialEq,
PartialOrd
,
Debug
)]
#[derive(Copy,
Clone,
PartialEq,
PartialOrd)]
pub
struct
Spot
{
pub
struct
Spot
{
inner
:
Instant
,
inner
:
Instant
,
}
}
impl
Debug
for
Spot
{
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
self
.inner
.fmt
(
f
)
}
}
impl
Spot
{
impl
Spot
{
pub
fn
now
()
->
Self
{
pub
fn
now
()
->
Self
{
Spot
{
Spot
{
inner
:
Instant
::
now
(),
inner
:
Instant
::
now
(),
}
}
}
}
pub
fn
as_secs
(
&
self
)
->
u64
{
/// Returns the amount of time elapsed since this instant was created.
self
.as_duration
()
.as_secs
()
pub
fn
elapsed
(
&
self
)
->
Duration
{
Spot
::
now
()
-
*
self
}
/// Returns the amount of time elapsed from another instant to this one,
/// or zero duration if that instant is later than this one.
pub
fn
duration_since
(
&
self
,
earlier
:
Spot
)
->
Duration
{
self
.checked_duration_since
(
earlier
)
.unwrap_or_default
()
}
/// Returns the amount of time elapsed from another instant to this one,
/// or None if that instant is later than this one.
///
/// Due to [monotonicity bugs], even under correct logical ordering of the passed `Instant`s,
/// this method can return `None`.
pub
fn
checked_duration_since
(
&
self
,
earlier
:
Spot
)
->
Option
<
Duration
>
{
self
.inner
.checked_duration_since
(
earlier
.inner
)
}
}
pub
fn
as_duration
(
&
self
)
->
Duration
{
/// Returns the amount of time elapsed from another instant to this one,
self
.inner
.elapsed
()
/// or zero duration if that instant is later than this one.
pub
fn
saturating_duration_since
(
&
self
,
earlier
:
Spot
)
->
Duration
{
self
.inner
.saturating_duration_since
(
earlier
.inner
)
.unwrap_or_default
()
}
}
/// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
/// otherwise.
pub
fn
checked_add
(
&
self
,
duration
:
Duration
)
->
Option
<
Spot
>
{
self
.inner
.checked_add
(
duration
)
.map
(|
inner
|
Spot
{
inner
})
}
/// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
/// otherwise.
pub
fn
checked_sub
(
&
self
,
duration
:
Duration
)
->
Option
<
Spot
>
{
self
.inner
.checked_sub
(
duration
)
.map
(|
inner
|
Spot
{
inner
})
}
}
pub
fn
elapsed
(
&
self
)
->
Duration
{
impl
Add
<
Duration
>
for
Spot
{
self
.as_duration
()
type
Output
=
Spot
;
/// # Panics
///
/// This function may panic if the resulting point in time cannot be represented by the
/// underlying data structure. See [`Spot::checked_add`] for a version without panic.
fn
add
(
self
,
other
:
Duration
)
->
Spot
{
self
.checked_add
(
other
)
.expect
(
"overflow when adding duration to instant"
)
}
}
impl
AddAssign
<
Duration
>
for
Spot
{
fn
add_assign
(
&
mut
self
,
other
:
Duration
)
{
*
self
=
*
self
+
other
;
}
}
impl
Sub
<
Duration
>
for
Spot
{
type
Output
=
Spot
;
fn
sub
(
self
,
other
:
Duration
)
->
Spot
{
self
.checked_sub
(
other
)
.expect
(
"overflow when subtracting duration from instant"
)
}
}
impl
SubAssign
<
Duration
>
for
Spot
{
fn
sub_assign
(
&
mut
self
,
other
:
Duration
)
{
*
self
=
*
self
-
other
;
}
}
}
}
impl
Sub
<
Spot
>
for
Spot
{
impl
Sub
<
Spot
>
for
Spot
{
type
Output
=
Duration
;
type
Output
=
Duration
;
fn
sub
(
self
,
rhs
:
Spot
)
->
Self
::
Output
{
/// Returns the amount of time elapsed from another instant to this one,
self
.inner
-
rhs
.inner
/// or zero duration if that instant is later than this one.
fn
sub
(
self
,
other
:
Spot
)
->
Duration
{
self
.duration_since
(
other
)
}
}
}
}
This diff is collapsed.
Click to expand it.
src/instant_web.rs
+
88
−
14
View file @
bd3445c2
use
std
::
ops
::
Sub
;
use
std
::
fmt
::{
Debug
,
Formatter
};
use
std
::
ops
::{
Add
,
AddAssign
,
Sub
,
SubAssign
};
use
std
::
time
::
Duration
;
use
std
::
time
::
Duration
;
#[derive(Copy,
Clone,
PartialEq,
PartialOrd
,
Debug
)]
#[derive(Copy,
Clone,
PartialEq,
PartialOrd)]
pub
struct
Spot
{
pub
struct
Spot
{
/// Millisecond offset from the Unix Epoch - equivalent to Date.now()
/// Millisecond offset from the Unix Epoch - equivalent to Date.now()
inner
:
f64
,
inner
:
f64
,
}
}
impl
Debug
for
Spot
{
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
self
.inner
.fmt
(
f
)
}
}
impl
Spot
{
impl
Spot
{
pub
fn
now
()
->
Self
{
pub
fn
now
()
->
Self
{
Spot
{
Spot
{
inner
:
js_sys
::
Date
::
now
(),
inner
:
js_sys
::
Date
::
now
(),
}
}
}
}
pub
fn
as_secs
(
&
self
)
->
u64
{
/// Returns the amount of time elapsed since this instant was created.
self
.as_duration
()
.as_secs
()
pub
fn
elapsed
(
&
self
)
->
Duration
{
Spot
::
now
()
-
*
self
}
}
pub
fn
as_duration
(
&
self
)
->
Duration
{
/// Returns the amount of time elapsed from another instant to this one,
Self
::
now
()
-
*
self
/// or zero duration if that instant is later than this one.
pub
fn
duration_since
(
&
self
,
earlier
:
Spot
)
->
Duration
{
self
.checked_duration_since
(
earlier
)
.unwrap_or_default
()
}
}
pub
fn
elapsed
(
&
self
)
->
Duration
{
/// Returns the amount of time elapsed from another instant to this one,
self
.as_duration
()
/// or None if that instant is later than this one.
///
/// Due to [monotonicity bugs], even under correct logical ordering of the passed `Instant`s,
/// this method can return `None`.
pub
fn
checked_duration_since
(
&
self
,
earlier
:
Spot
)
->
Option
<
Duration
>
{
if
earlier
.inner
>
self
.inner
{
None
}
else
{
let
millis
=
(
self
.inner
-
earlier
.inner
);
Some
(
Duration
::
from_secs_f64
(
millis
*
1000.0
))
}
}
/// Returns the amount of time elapsed from another instant to this one,
/// or zero duration if that instant is later than this one.
pub
fn
saturating_duration_since
(
&
self
,
earlier
:
Spot
)
->
Duration
{
self
.checked_duration_since
(
earlier
)
.unwrap_or_default
()
}
/// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
/// otherwise.
pub
fn
checked_add
(
&
self
,
duration
:
Duration
)
->
Option
<
Spot
>
{
let
duration_millis
=
duration
.as_secs_f64
()
/
1000.0
;
Some
(
Spot
{
inner
:
self
.inner
+
duration_millis
})
}
/// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
/// otherwise.
pub
fn
checked_sub
(
&
self
,
duration
:
Duration
)
->
Option
<
Spot
>
{
let
duration_millis
=
duration
.as_secs_f64
()
/
1000.0
;
if
duration_millis
>
self
.inner
{
None
}
else
{
Some
(
Spot
{
inner
:
self
.inner
-
duration_millis
})
}
}
}
impl
Add
<
Duration
>
for
Spot
{
type
Output
=
Spot
;
/// # Panics
///
/// This function may panic if the resulting point in time cannot be represented by the
/// underlying data structure. See [`Spot::checked_add`] for a version without panic.
fn
add
(
self
,
other
:
Duration
)
->
Spot
{
self
.checked_add
(
other
)
.expect
(
"overflow when adding duration to instant"
)
}
}
impl
AddAssign
<
Duration
>
for
Spot
{
fn
add_assign
(
&
mut
self
,
other
:
Duration
)
{
*
self
=
*
self
+
other
;
}
}
}
}
impl
Sub
<
Duration
>
for
Spot
{
type
Output
=
Spot
;
fn
sub
(
self
,
other
:
Duration
)
->
Spot
{
self
.checked_sub
(
other
)
.expect
(
"overflow when subtracting duration from instant"
)
}
}
impl
SubAssign
<
Duration
>
for
Spot
{
fn
sub_assign
(
&
mut
self
,
other
:
Duration
)
{
*
self
=
*
self
-
other
;
}
}
impl
Sub
<
Spot
>
for
Spot
{
impl
Sub
<
Spot
>
for
Spot
{
type
Output
=
Duration
;
type
Output
=
Duration
;
fn
sub
(
self
,
rhs
:
Spot
)
->
Self
::
Output
{
/// Returns the amount of time elapsed from another instant to this one,
let
diff
=
(
self
.inner
-
rhs
.inner
)
.max
(
0.0
);
/// or zero duration if that instant is later than this one.
let
secs
=
(
diff
as
u64
)
/
1_000
;
fn
sub
(
self
,
other
:
Spot
)
->
Duration
{
let
nanos
=
(((
diff
as
u64
)
%
1_000
)
as
u32
)
*
1_000_000
;
self
.duration_since
(
other
)
Duration
::
new
(
secs
,
nanos
)
}
}
}
}
This diff is collapsed.
Click to expand it.
src/lib.rs
+
2
−
2
View file @
bd3445c2
#[cfg(not(target_
arch
=
"wasm
32
"
))]
#[cfg(not(target_
family
=
"wasm"
))]
mod
instant_desktop
;
mod
instant_desktop
;
#[cfg(target_
arch
=
"wasm
32
"
)]
//
#[cfg(target_
family
= "wasm")]
mod
instant_web
;
mod
instant_web
;
#[cfg(not(target_arch
=
"wasm32"
))]
#[cfg(not(target_arch
=
"wasm32"
))]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment