Skip to content

Commit d7c27a6

Browse files
fix(rust): support Console.Error.WriteLine (#5117)
Co-authored-by: Mangel Maxime <me@mangelmaxime.fr>
1 parent 7442d68 commit d7c27a6

6 files changed

Lines changed: 150 additions & 1 deletion

File tree

‎src/Fable.Transforms/Rust/Fable2Rust.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ module TypeInfo =
958958
let getAbstractClassImportName com ctx (entRef: Fable.EntityRef) =
959959
match entRef.FullName with
960960
| "System.Text.Encoding" -> getLibraryImportName com ctx "Encoding" "Encoding"
961+
| "System.IO.TextWriter" -> getLibraryImportName com ctx "Console" "TextWriter"
961962
| _ -> getEntityFullName com ctx entRef
962963

963964
let transformAbstractClassType com ctx (entRef: Fable.EntityRef) genArgs : Rust.Ty =

‎src/Fable.Transforms/Rust/Replacements.fs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,17 @@ let objectHash (com: ICompiler) ctx r t (arg: Expr) =
552552
| Array _ -> referenceHash com ctx r t arg
553553
| _ -> getHashCode com ctx r t arg
554554

555+
let private (|TextWriterExpr|_|) (expr: Expr) =
556+
match expr with
557+
| MaybeCasted(ExprType(DeclaredType(ent, _)) as expr) when ent.FullName = "System.IO.TextWriter" -> Some expr
558+
| _ -> None
559+
555560
let referenceEquals (com: ICompiler) ctx r (left: Expr) (right: Expr) =
556561
match left, right with
557562
| Value(Null _, _), o
558563
| o, Value(Null _, _) -> libCall com r Boolean "Native" "is_null" [ makeRef o ]
564+
| TextWriterExpr left, TextWriterExpr right ->
565+
libCall com r Boolean "Console" "writerEquals" [ makeRef left; makeRef right ]
559566
| _ ->
560567
match left.Type with
561568
| Boolean
@@ -2652,11 +2659,20 @@ let convert (com: ICompiler) (ctx: Context) r t (i: CallInfo) (_: Expr option) (
26522659

26532660
let console (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
26542661
match i.CompiledName with
2655-
| "get_Out" -> typedObjExpr t [] |> Some // empty object
2662+
| "get_Out" -> libCall com r t "Console" "getOut" [] |> Some
2663+
| "get_Error" -> libCall com r t "Console" "getError" [] |> Some
26562664
| "Write" -> "printf!" |> emitFormat com r t args |> Some
26572665
| "WriteLine" -> "printfn!" |> emitFormat com r t args |> Some
26582666
| _ -> None
26592667

2668+
let textWriter (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
2669+
match i.CompiledName, thisArg, args with
2670+
| "WriteLine", Some writer, [ message ] ->
2671+
let message = toString com ctx r [ message ]
2672+
libCall com r t "Console" "writeLine" [ writer; message ] |> Some
2673+
| "WriteLine", Some writer, [] -> libCall com r t "Console" "writeLineEmpty" [ writer ] |> Some
2674+
| _ -> None
2675+
26602676
let debug (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
26612677
match i.CompiledName with
26622678
| "Write" -> "printf!" |> emitFormat com r t args |> Some
@@ -3699,6 +3715,7 @@ let private replacedModules =
36993715
Types.decimal, decimals
37003716
"System.Convert", convert
37013717
"System.Console", console
3718+
"System.IO.TextWriter", textWriter
37023719
"System.Diagnostics.Debug", debug
37033720
"System.Diagnostics.Debugger", debug
37043721
Types.datetime, dateTimes
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
pub mod Console_ {
2+
use crate::Native_::{LrcPtr, NullableRef, eprint, print};
3+
use crate::String_::string;
4+
5+
#[derive(Clone, Debug)]
6+
pub enum TextWriter {
7+
Out,
8+
Error,
9+
}
10+
11+
impl PartialEq for TextWriter {
12+
fn eq(&self, other: &Self) -> bool {
13+
core::ptr::eq(self, other)
14+
}
15+
}
16+
17+
#[cfg(not(feature = "threaded"))]
18+
type WriterCell = crate::Native_::OnceInit<LrcPtr<TextWriter>>;
19+
#[cfg(feature = "threaded")]
20+
type WriterCell = std::sync::OnceLock<LrcPtr<TextWriter>>;
21+
22+
static OUT: WriterCell = WriterCell::new();
23+
static ERROR: WriterCell = WriterCell::new();
24+
25+
pub fn getOut() -> LrcPtr<TextWriter> {
26+
OUT.get_or_init(|| LrcPtr::new(TextWriter::Out)).clone()
27+
}
28+
29+
pub fn getError() -> LrcPtr<TextWriter> {
30+
ERROR.get_or_init(|| LrcPtr::new(TextWriter::Error)).clone()
31+
}
32+
33+
fn as_writer(writer: &LrcPtr<TextWriter>) -> &TextWriter {
34+
writer
35+
}
36+
37+
pub fn writerEquals(left: &LrcPtr<TextWriter>, right: &LrcPtr<TextWriter>) -> bool {
38+
match (left.is_null(), right.is_null()) {
39+
(true, true) => true,
40+
(false, false) => core::ptr::eq(as_writer(left), as_writer(right)),
41+
_ => false,
42+
}
43+
}
44+
45+
fn as_text(message: &string) -> &str {
46+
if message.is_null() {
47+
""
48+
} else {
49+
message.as_str()
50+
}
51+
}
52+
53+
pub fn writeLine(writer: LrcPtr<TextWriter>, message: string) {
54+
match as_writer(&writer) {
55+
TextWriter::Out => print!("{}\n", as_text(&message)),
56+
TextWriter::Error => eprint!("{}\n", as_text(&message)),
57+
}
58+
}
59+
60+
pub fn writeLineEmpty(writer: LrcPtr<TextWriter>) {
61+
writeLine(writer, string(""));
62+
}
63+
}

‎src/fable-library-rust/src/lib.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let _imports () =
88
importAll "./BitConverter.rs"
99
importAll "./Char.rs"
1010
importAll "./Convert.rs"
11+
importAll "./Console.rs"
1112
importAll "./DateOnly.rs"
1213
importAll "./DateTime.rs"
1314
importAll "./DateTimeOffset.rs"

‎tests/Rust/Fable.Tests.Rust.fsproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<Compile Include="tests/src/ClassTests.fs" />
4141
<Compile Include="tests/src/ClosureTests.fs" />
4242
<Compile Include="tests/src/ComparisonTests.fs" />
43+
<Compile Include="tests/src/ConsoleTests.fs" />
4344
<Compile Include="tests/src/ControlFlowTests.fs" />
4445
<Compile Include="tests/src/ConvertTests.fs" />
4546
<Compile Include="tests/src/CustomOperatorTests.fs" />
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module Fable.Tests.ConsoleTests
2+
3+
open Util.Testing
4+
open System
5+
open System.IO
6+
7+
let private writeLineTo (writer: TextWriter) (s: string) = writer.WriteLine(s)
8+
9+
let private sameWriter (a: TextWriter) (b: TextWriter) = obj.ReferenceEquals(a, b)
10+
11+
let private equalWriter (a: TextWriter) (b: TextWriter) = a = b
12+
13+
[<Fact>]
14+
let ``Console.Out and Console.Error WriteLine work`` () =
15+
Console.Error.WriteLine("Rust stderr")
16+
Console.Out.WriteLine("Rust stdout")
17+
Console.Error.WriteLine()
18+
Console.Out.WriteLine()
19+
20+
[<Fact>]
21+
let ``Console.Out and Console.Error are singletons`` () =
22+
obj.ReferenceEquals(Console.Out, Console.Out) |> equal true
23+
obj.ReferenceEquals(Console.Error, Console.Error) |> equal true
24+
obj.ReferenceEquals(Console.Out, Console.Error) |> equal false
25+
(Console.Out = Console.Out) |> equal true
26+
(Console.Error = Console.Error) |> equal true
27+
(Console.Out = Console.Error) |> equal false
28+
sameWriter Console.Out Console.Out |> equal true
29+
sameWriter Console.Out Console.Error |> equal false
30+
31+
[<Fact>]
32+
let ``TextWriter equality is reference equality`` () =
33+
let nullWriter: TextWriter = null
34+
sameWriter nullWriter Console.Out |> equal false
35+
sameWriter Console.Out nullWriter |> equal false
36+
sameWriter nullWriter Console.Error |> equal false
37+
sameWriter Console.Error nullWriter |> equal false
38+
sameWriter nullWriter nullWriter |> equal true
39+
sameWriter Console.Out Console.Out |> equal true
40+
sameWriter Console.Error Console.Error |> equal true
41+
sameWriter Console.Out Console.Error |> equal false
42+
sameWriter Console.Error Console.Out |> equal false
43+
equalWriter nullWriter Console.Out |> equal false
44+
equalWriter Console.Out nullWriter |> equal false
45+
equalWriter nullWriter Console.Error |> equal false
46+
equalWriter Console.Error nullWriter |> equal false
47+
equalWriter nullWriter nullWriter |> equal true
48+
equalWriter Console.Out Console.Out |> equal true
49+
equalWriter Console.Error Console.Error |> equal true
50+
equalWriter Console.Out Console.Error |> equal false
51+
(nullWriter, 1) = (Console.Out, 1) |> equal false
52+
(Console.Error, 1) = (nullWriter, 1) |> equal false
53+
(nullWriter, 1) = (nullWriter, 1) |> equal true
54+
(Console.Out, 1) = (Console.Out, 1) |> equal true
55+
(Console.Out, 1) = (Console.Error, 1) |> equal false
56+
57+
[<Fact>]
58+
let ``TextWriter can be used as a type`` () =
59+
writeLineTo Console.Error "TextWriter parameter stderr"
60+
writeLineTo Console.Out "TextWriter parameter stdout"
61+
let mutable writer = Console.Out
62+
sameWriter writer Console.Out |> equal true
63+
writer.WriteLine("TextWriter mutable stdout")
64+
writer <- Console.Error
65+
sameWriter writer Console.Error |> equal true
66+
writer.WriteLine("TextWriter mutable stderr")

0 commit comments

Comments
 (0)