Skip to content
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/Fable.Transforms/Rust/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3023,6 +3023,9 @@ let asyncBuilder (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
| "Return", _, _ ->
Helper.LibCall(com, "AsyncBuilder", "r_return", t, args, i.SignatureArgTypes, ?loc = r)
|> Some
| "ReturnFrom", _, _ ->
Helper.LibCall(com, "AsyncBuilder", "return_from", t, args, i.SignatureArgTypes, ?loc = r)
|> Some
| "Zero", _, _ ->
Helper.LibCall(com, "AsyncBuilder", "zero", t, args, i.SignatureArgTypes, ?loc = r)
|> Some
Expand Down
4 changes: 4 additions & 0 deletions src/fable-library-rust/src/Async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ pub mod AsyncBuilder_ {
})
}

pub fn return_from<T: Send + Sync + 'static>(computation: Arc<Async<T>>) -> Arc<Async<T>> {
computation
}

pub fn zero<T: Send + Sync + 'static>() -> Arc<Async<()>> {
r_return(())
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Rust/tests/src/AsyncTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,55 @@ let shouldConvertTaskToASyncAndEvalCorrectly () =
let t = task { return 1 } |> Async.AwaitTask
t |> Async.RunSynchronously |> equal 1

[<Fact>]
let ``return! should compile in async CE`` () =
let inner () = async { return 42 }
let outer () = async { return! inner () }
let result = Async.RunSynchronously (outer ())
result |> equal 42

[<Fact>]
let ``return! works in recursive async CE`` () =
let rec loop n = async {
if n <= 0 then return 0
else return! loop (n - 1)
}
let result = Async.RunSynchronously (loop 5)
result |> equal 0

[<Fact>]
let ``return! is transparent through multiple layers`` () =
// Verifies return_from is identity: chaining return! does not double-wrap the computation
// or alter the value in any way.
let inner () = async { return 7 }
let passthrough (comp: Async<int>) = async { return! comp }
let result =
inner ()
|> passthrough
|> passthrough
|> passthrough
|> Async.RunSynchronously
result |> equal 7

[<Fact>]
let ``return! propagates value from async built with bind`` () =
// Verifies that a computation produced via let! / return is correctly
// passed through return!, not just a literal return value.
let doubled x = async {
let! v = async { return x }
return v * 2
}
let outer () = async { return! doubled 21 }
Async.RunSynchronously (outer ()) |> equal 42

[<Fact>]
let ``return! propagates error Result from inner async`` () =
// Both Ok and Error variants must flow through return! unchanged.
let inner (result: Result<int, string>) = async { return result }
let outer (result: Result<int, string>) = async { return! inner result }
Async.RunSynchronously (outer (Ok 99)) |> equal (Ok 99)
Async.RunSynchronously (outer (Error "oops")) |> equal (Error "oops")

// [<Fact>]
// let shouldExecAsParallelStructurallyCorrect () =
// let t = Async.Parallel [
Expand Down
Loading