Skip to content

Commit

Permalink
[Rust] Added Async.Sleep and test (#3907)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave authored Sep 29, 2024
1 parent a84cdd8 commit f2cd06f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [Rust] Added support for Dictionary/HashSet comparers (by @ncave)
* [Rust] Updated support for interface object expressions (by @ncave)
* [Rust] Added missing ResizeArray methods and tests (by @ncave)
* [Rust] Added Async.Sleep and test (by @ncave)

### Fixed

Expand Down
13 changes: 7 additions & 6 deletions src/fable-library-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ lrc_ptr = []
no_std = ["dep:hashbrown"]
regexp = ["dep:regex"]
static_do_bindings = ["dep:startup"]
threaded = ["atomic", "dep:futures"]
threaded = ["atomic", "dep:futures", "dep:futures-timer"]
default = ["bigint", "datetime", "decimal", "enum_func", "enum_string", "guid", "regexp"]

[dependencies]
startup = { version = "0.1", path = "vendored/startup", optional = true }
chrono = { version = "0.4", optional = true }
futures = { version = "0.3", features = ["executor", "thread-pool"], default-features = false, optional = true }
futures-timer = { version = "3.0", optional = true }
hashbrown = { version = "0.14", optional = true }
num-bigint = { version = "0.4", optional = true }
num-integer = { version = "0.1", optional = true }
num-traits = { version = "0.2", optional = true }
rust_decimal = { version = "1.35", features = ["maths"], default-features = false, optional = true }
futures = { version = "0.3", features = ["executor", "thread-pool"], optional = true }
uuid = { version = "1.10", features = ["v4"], default-features = false, optional = true }
chrono = { version = "0.4", optional = true }
regex = { version = "1.10", optional = true }
rust_decimal = { version = "1.36", features = ["maths"], default-features = false, optional = true }
startup = { version = "0.1", path = "vendored/startup", optional = true }
uuid = { version = "1.10", features = ["v4"], default-features = false, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
9 changes: 9 additions & 0 deletions src/fable-library-rust/src/Async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod Async_ {
use futures::executor::{self, LocalPool};
use futures::lock::Mutex;
use futures::FutureExt;
use futures_timer::Delay;

use super::Task_::Task;

Expand Down Expand Up @@ -38,6 +39,14 @@ pub mod Async_ {
}
}

pub fn sleep(milliseconds: i32) -> Arc<Async<()>> {
let fut = Delay::new(Duration::from_millis(milliseconds as u64));
let a: Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>> = Box::pin(fut);
Arc::from(Async {
future: Arc::from(Mutex::from(a)),
})
}

pub fn startAsTask<T: Clone + Send + Sync + 'static>(a: Arc<Async<T>>) -> Arc<Task<T>> {
let unitFut = async move {
let mut res = a.future.lock().await;
Expand Down
2 changes: 1 addition & 1 deletion tests/Js/Main/AsyncTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ let tests =
equal x "ABCDEF"
}

testCaseAsync "Async.StartChild applys timeout" <| fun () ->
testCaseAsync "Async.StartChild applies timeout" <| fun () ->
async {
let mutable x = ""

Expand Down
22 changes: 19 additions & 3 deletions tests/Rust/tests/src/AsyncTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ let shouldExecuteTask () =
// t.Join()
// x |> equal 2


// [<Fact>]
// let monitorShouldWorkWithSystemObj () =
// let o = new System.Object() // todo - this doesn't work, and outputs unit
Expand Down Expand Up @@ -175,6 +174,24 @@ let ``Lock should return result`` () =
// do t.Result
// x |> equal 2

[<Fact>]
let ``Async.Sleep works`` () =
let mutable s = ""
let a1 =
async {
do! Async.Sleep(200)
s <- s + "200"
}
let a2 =
async {
do! Async.Sleep(100)
s <- s + "100"
}
let t1 = a1 |> Async.StartAsTask
let t2 = a2 |> Async.StartAsTask
let r1 = t1.Result
let r2 = t2.Result
s |> equal "100200"

// type DisposableAction(f) =
// interface IDisposable with
Expand Down Expand Up @@ -588,7 +605,6 @@ let ``Lock should return result`` () =
// Async.StartWithContinuations(work, (fun r -> result <- r), ignore, ignore)
// equal result 42


// [<Fact>]
// let ``Deep recursion with async doesn't cause stack overflow`` () =
// async {
Expand Down Expand Up @@ -744,7 +760,7 @@ let ``Lock should return result`` () =
// |> Async.StartImmediate

// [<Fact>]
// let ``Async.StartChild applys timeout`` () =
// let ``Async.StartChild applies timeout`` () =
// async {
// let mutable x = ""
// let task = async {
Expand Down
22 changes: 11 additions & 11 deletions tests/Rust/tests/src/TypeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ let inline show< ^T when ^T : (member show : unit -> string)> (x:^T) : string =
let inline showStatic< ^T when ^T : (static member show : ^T -> string)> (x:^T) : string =
(^T : (static member show : ^T -> string) (x))

// [<AllowNullLiteral>]
// type Serializable(?i: int) =
// let mutable deserialized = false
// let mutable publicValue = 1
// let mutable privateValue = defaultArg i 0
// member x.PublicValue
// with get() = publicValue
// and set(i) = deserialized <- true; publicValue <- i
// override x.ToString() =
// sprintf "Public: %i - Private: %i - Deserialized: %b"
// publicValue privateValue deserialized
[<AllowNullLiteral>]
type Serializable(?i: int) =
let mutable deserialized = false
let mutable publicValue = 1
let mutable privateValue = defaultArg i 0
member x.PublicValue
with get() = publicValue
and set(i) = deserialized <- true; publicValue <- i
override x.ToString() =
sprintf "Public: %i - Private: %i - Deserialized: %b"
publicValue privateValue deserialized

type SecondaryCons(x: int) =
new () = SecondaryCons(5)
Expand Down

0 comments on commit f2cd06f

Please sign in to comment.