Skip to content

Commit

Permalink
[Rust] Updated some collections equality (#3945)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave authored Oct 27, 2024
1 parent 974f63e commit 09117e0
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

* [Rust] Updated derived traits mapping (by @ncave)
* [Rust] Updated some collections equality (by @ncave)
* [Rust] Fixed try finally handler order of execution (by @ncave)
* [JS/TS/Python/Rust] Fixed String.StartsWith/EndsWith (#3934) (by @ncave)
* [All/Rust] Removed Regex.Replace from hot paths (by @ncave)
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3529,6 +3529,7 @@ let private replacedModules =
"System.Text.StringBuilder", bclType
Types.array, arrays
Types.list, lists
"Microsoft.FSharp.Collections.ArrayModule.Parallel", arrayModule
"Microsoft.FSharp.Collections.ArrayModule", arrayModule
"Microsoft.FSharp.Collections.ListModule", listModule
"Microsoft.FSharp.Collections.HashIdentity", fsharpModule
Expand Down
12 changes: 9 additions & 3 deletions src/Fable.Transforms/Rust/Fable2Rust.fs
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ module TypeInfo =
let isNonDefaultableType (com: IRustCompiler) entNames typ =
match typ with
// TODO: more undefaultable types?
| Fable.LambdaType _
| Fable.DelegateType _ -> true
// | Fable.LambdaType _
// | Fable.DelegateType _ -> true
| _ -> hasTypeOfType com isNonDefaultableType isNonDefaultableEntity entNames typ

let isNonDefaultableEntity com entNames (ent: Fable.Entity) =
Expand Down Expand Up @@ -1164,6 +1164,11 @@ module Util =
| Replacements.Util.IsEntity (Types.iformattable) _ -> Some()
| _ -> None

let (|IComparable|_|) =
function
| Replacements.Util.IsEntity (Types.icomparableGeneric) (_, [ genArg ]) -> Some(genArg)
| _ -> None

let (|IEquatable|_|) =
function
| Replacements.Util.IsEntity (Types.iequatableGeneric) (_, [ genArg ]) -> Some(genArg)
Expand Down Expand Up @@ -3864,7 +3869,8 @@ module Util =
| Fable.Constraint.CoercesTo(targetType) ->
match targetType with
| IFormattable -> [ makeGenBound ("core" :: "fmt" :: "Display" :: []) [] ]
| IEquatable _ -> [ makeGenBound ("core" :: "hash" :: "Hash" :: []) []; makeRawBound "PartialEq" ]
| IComparable _ -> [ makeRawBound "PartialOrd" ]
| IEquatable _ -> [ makeRawBound "PartialEq" ]
| Fable.DeclaredType(entRef, genArgs) ->
let ent = com.GetEntity(entRef)

Expand Down
7 changes: 5 additions & 2 deletions src/Fable.Transforms/Rust/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ let isCompatibleWithNativeComparison =
| String
| Number _
| GenericParam _
| Array _
| List _
// | Array _
// | List _
| Builtin(BclGuid) -> true
| Builtin(BclTimeSpan) -> true
| _ -> false
Expand Down Expand Up @@ -3377,6 +3377,7 @@ let private replacedModules =
"System.Text.StringBuilder", stringBuilder
Types.array, arrays
Types.list, lists
"Microsoft.FSharp.Collections.ArrayModule.Parallel", arrayModule
"Microsoft.FSharp.Collections.ArrayModule", arrayModule
"Microsoft.FSharp.Collections.ListModule", listModule
"Microsoft.FSharp.Collections.HashIdentity", fsharpModule
Expand All @@ -3386,6 +3387,8 @@ let private replacedModules =
Types.keyValuePair, keyValuePairs
"System.Collections.Generic.Comparer`1", bclType
"System.Collections.Generic.EqualityComparer`1", bclType
Types.iequatableGeneric, valueTypes
Types.icomparableGeneric, valueTypes
Types.dictionary, dictionaries
Types.idictionary, dictionaries
Types.ireadonlydictionary, dictionaries
Expand Down
4 changes: 2 additions & 2 deletions src/fable-library-rust/src/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ let compareTo (source1: 'T[]) (source2: 'T[]) =

res

let equals (source1: 'T[]) (source2: 'T[]) =
let equals<'T when 'T :> System.IEquatable<'T>> (source1: 'T[]) (source2: 'T[]) =
// LanguagePrimitives.GenericEquality source1 source2
let len1 = source1.Length
let len2 = source2.Length
Expand All @@ -181,7 +181,7 @@ let equals (source1: 'T[]) (source2: 'T[]) =
let mutable res = true

while res && i < len1 do
res <- source1[i] = source2[i]
res <- source1[i].Equals(source2[i])
i <- i + 1

res
Expand Down
4 changes: 2 additions & 2 deletions src/fable-library-rust/src/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,14 @@ let compareTo (xs: 'T list) (ys: 'T list) =
// LanguagePrimitives.GenericComparison xs ys
compareWith compare xs ys

let rec equals (xs: 'T list) (ys: 'T list) =
let rec equals<'T when 'T :> System.IEquatable<'T>> (xs: 'T list) (ys: 'T list) =
// LanguagePrimitives.GenericEquality xs ys
match (isEmpty xs), (isEmpty ys) with
| true, true -> true
| true, false -> false
| false, true -> false
| false, false ->
if (head xs) = (head ys) then
if (head xs).Equals(head ys) then
equals (tail xs) (tail ys)
else
false
Expand Down
6 changes: 3 additions & 3 deletions src/fable-library-rust/src/Seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ let compareTo (xs: 'T seq) (ys: 'T seq) =
// LanguagePrimitives.GenericComparison xs ys
compareWith compare xs ys

let equals (xs: 'T seq) (ys: 'T seq) =
let equals<'T when 'T :> System.IEquatable<'T>> (xs: 'T seq) (ys: 'T seq) =
// LanguagePrimitives.GenericEquality xs ys
use e1 = ofSeq xs
use e2 = ofSeq ys
Expand All @@ -541,13 +541,13 @@ let equals (xs: 'T seq) (ys: 'T seq) =
let mutable b2 = e2.MoveNext()

while res && b1 && b2 do
res <- e1.Current = e2.Current
res <- e1.Current.Equals(e2.Current)

if res then
b1 <- e1.MoveNext()
b2 <- e2.MoveNext()

res
res && b1 = b2

// let enumerateFromFunctions create moveNext current =
// generate
Expand Down
8 changes: 4 additions & 4 deletions tests/Rust/tests/src/ArrayTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ let ``Array comparison works`` () =
a1 < a2 |> equal false
a1 < a3 |> equal true
a1 < a4 |> equal true
a1 > a1 |> equal false
a1 > a2 |> equal false
a1 > a3 |> equal false
a1 > a4 |> equal false
a3 < a4 |> equal true
a3 < a2 |> equal false
a4 < a2 |> equal false
a4 < a3 |> equal false

[<Fact>]
let ``Array compare works`` () =
Expand Down
8 changes: 4 additions & 4 deletions tests/Rust/tests/src/ListTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ let ``List comparison works`` () =
a1 < a2 |> equal false
a1 < a3 |> equal true
a1 < a4 |> equal true
a1 > a1 |> equal false
a1 > a2 |> equal false
a1 > a3 |> equal false
a1 > a4 |> equal false
a3 < a4 |> equal false
a3 < a2 |> equal false
a4 < a2 |> equal false
a4 < a3 |> equal true

[<Fact>]
let ``List compare works`` () =
Expand Down
48 changes: 24 additions & 24 deletions tests/Rust/tests/src/SeqTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,31 @@ let ``Seq.empty works`` () =
let xs = Seq.empty<int>
Seq.length xs |> equal 0

// [<Fact>]
// let ``Seq equality works`` () =
// let a1 = seq {1; 2; 3}
// let a2 = seq {1; 2; 3}
// let a3 = seq {1; 2; 4}
// let a4 = seq {1; 2; 3; 4}
// a1 = a1 |> equal true
// // a1 = a2 |> equal false //TODO: reference equality
// a1 = a3 |> equal false
// a1 = a4 |> equal false
// a1 <> a1 |> equal false
// // a1 <> a2 |> equal true //TODO: reference inequality
// a1 <> a3 |> equal true
// a1 <> a4 |> equal true
[<Fact>]
let ``Seq equality works`` () =
let a1 = seq {1; 2; 3}
let a2 = seq {1; 2; 3}
let a3 = seq {1; 2; 4}
let a4 = seq {1; 2; 3; 4}
a1 = a1 |> equal true
// a1 = a2 |> equal false //TODO: reference equality
a1 = a3 |> equal false
a1 = a4 |> equal false
a1 <> a1 |> equal false
// a1 <> a2 |> equal true //TODO: reference inequality
a1 <> a3 |> equal true
a1 <> a4 |> equal true

// [<Fact>]
// let ``Seq.Equals works`` () =
// let a1 = seq {1; 2; 3}
// let a2 = seq {1; 2; 3}
// let a3 = seq {1; 2; 4}
// let a4 = seq {1; 2; 3; 4}
// a1.Equals(a1) |> equal true
// // a1.Equals(a2) |> equal false //TODO: reference equality
// a1.Equals(a3) |> equal false
// a1.Equals(a4) |> equal false
[<Fact>]
let ``Seq.Equals works`` () =
let a1 = seq {1; 2; 3}
let a2 = seq {1; 2; 3}
let a3 = seq {1; 2; 4}
let a4 = seq {1; 2; 3; 4}
a1.Equals(a1) |> equal true
// a1.Equals(a2) |> equal false //TODO: reference equality
a1.Equals(a3) |> equal false
a1.Equals(a4) |> equal false

[<Fact>]
let ``Seq.compareWith works`` () =
Expand Down

0 comments on commit 09117e0

Please sign in to comment.