Skip to content

Commit 8b18ae5

Browse files
committed
fable-library-rust update
1 parent 53fd6cb commit 8b18ae5

File tree

9 files changed

+295
-461
lines changed

9 files changed

+295
-461
lines changed

src/Fable.Transforms/Rust/Replacements.fs

+10-7
Original file line numberDiff line numberDiff line change
@@ -2102,8 +2102,10 @@ let arrays (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: E
21022102

21032103
let arrayModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Expr option) (args: Expr list) =
21042104
match i.CompiledName, args with
2105-
| "ToSeq", [arg] -> Some arg
2106-
| "OfSeq", [arg] -> toArray r t arg |> Some
2105+
| "ToSeq", [arg] ->
2106+
Helper.LibCall(com, "Seq", "ofArray", t, args, i.SignatureArgTypes, ?loc=r) |> Some
2107+
| "OfSeq", [arg] ->
2108+
Helper.LibCall(com, "Seq", "toArray", t, args, i.SignatureArgTypes, ?loc=r) |> Some
21072109
| "OfList", [arg] ->
21082110
Helper.LibCall(com, "List", "toArray", t, args, i.SignatureArgTypes, ?loc=r) |> Some
21092111
| "ToList", args ->
@@ -2155,15 +2157,16 @@ let lists (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: Ex
21552157

21562158
let listModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Expr option) (args: Expr list) =
21572159
match i.CompiledName, args with
2158-
| "IsEmpty", [x] -> Test(x, ListTest false, r) |> Some
2160+
| "IsEmpty", [arg] -> Test(arg, ListTest false, r) |> Some
21592161
| "Empty", _ -> NewList(None, (genArg com ctx r 0 i.GenericArgs)) |> makeValue r |> Some
2160-
| "Singleton", [x] ->
2161-
NewList(Some(x, Value(NewList(None, t), None)), (genArg com ctx r 0 i.GenericArgs)) |> makeValue r |> Some
2162+
| "Singleton", [arg] ->
2163+
NewList(Some(arg, Value(NewList(None, t), None)), (genArg com ctx r 0 i.GenericArgs)) |> makeValue r |> Some
21622164
// Use a cast to give it better chances of optimization (e.g. converting list
21632165
// literals to arrays) after the beta reduction pass
2164-
| "ToSeq", [x] ->
2165-
//toSeq t x |> Some
2166+
| "ToSeq", [arg] ->
21662167
Helper.LibCall(com, "Seq", "ofList", t, args, i.SignatureArgTypes, ?loc=r) |> Some
2168+
| "OfSeq", [arg] ->
2169+
Helper.LibCall(com, "Seq", "toList", t, args, i.SignatureArgTypes, ?loc=r) |> Some
21672170
| ("Distinct" | "DistinctBy" | "Except" | "GroupBy" | "CountBy" as meth), args ->
21682171
let meth = Naming.lowerFirst meth
21692172
// let args = injectArg com ctx r "Seq2" meth i.GenericArgs args

src/fable-library-rust/src/Array.fs

+89-88
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,12 @@ let indexed (source: 'T[]) =
188188
target.[i] <- (i, source.[i])
189189
target
190190

191-
// let truncate (count: int) (source: 'T[]): 'T[] =
192-
// let count = max 0 count
193-
// subArrayImpl source 0 count
191+
let truncate (count: int) (source: 'T[]): 'T[] =
192+
let count =
193+
if count < 0 then 0
194+
elif count > source.Length then source.Length
195+
else count
196+
getSubArray source 0 count
194197

195198
// let concat (arrays: 'T[] seq) ([<Inject>] cons: Cons<'T>): 'T[] =
196199
// let arrays =
@@ -288,7 +291,7 @@ let initialize count initializer =
288291
// if count = 0 then
289292
// allocateArrayFromCons cons 0
290293
// else
291-
// subArrayImpl source 0 count
294+
// getSubArray source 0 count
292295

293296
// let takeWhile predicate (source: 'T[]) ([<Inject>] cons: Cons<'T>) =
294297
// let mutable count = 0
@@ -297,7 +300,7 @@ let initialize count initializer =
297300
// if count = 0 then
298301
// allocateArrayFromCons cons 0
299302
// else
300-
// subArrayImpl source 0 count
303+
// getSubArray source 0 count
301304

302305
// let addInPlace (x: 'T) (source: 'T[]) =
303306
// // if isTypedArrayImpl source then invalidArg "source" "Typed arrays not supported"
@@ -393,86 +396,81 @@ let initialize count initializer =
393396
// iFalse <- iFalse + 1
394397
// res1 |> truncate iTrue, res2 |> truncate iFalse
395398

396-
// let find (predicate: 'T -> bool) (source: 'T[]): 'T =
397-
// match findImpl predicate source with
398-
// | Some res -> res
399-
// | None -> indexNotFound()
400-
401-
// let tryFind (predicate: 'T -> bool) (source: 'T[]): 'T option =
402-
// findImpl predicate source
403-
404-
// let findIndex (predicate: 'T -> bool) (source: 'T[]): int =
405-
// match findIndexImpl predicate source with
406-
// | index when index > -1 -> index
407-
// | _ -> indexNotFound()
408-
409-
// let tryFindIndex (predicate: 'T -> bool) (source: 'T[]): int option =
410-
// match findIndexImpl predicate source with
411-
// | index when index > -1 -> Some index
412-
// | _ -> None
413-
414-
// let pick chooser (source: 'T[]) =
415-
// let rec loop i =
416-
// if i >= source.Length then
417-
// indexNotFound()
418-
// else
419-
// match chooser source.[i] with
420-
// | None -> loop(i+1)
421-
// | Some res -> res
422-
// loop 0
423-
424-
// let tryPick chooser (source: 'T[]) =
425-
// let rec loop i =
426-
// if i >= source.Length then None else
427-
// match chooser source.[i] with
428-
// | None -> loop(i+1)
429-
// | res -> res
430-
// loop 0
431-
432-
// let findBack predicate (source: 'T[]) =
433-
// let rec loop i =
434-
// if i < 0 then indexNotFound()
435-
// elif predicate source.[i] then source.[i]
436-
// else loop (i - 1)
437-
// loop (source.Length - 1)
438-
439-
// let tryFindBack predicate (source: 'T[]) =
440-
// let rec loop i =
441-
// if i < 0 then None
442-
// elif predicate source.[i] then Some source.[i]
443-
// else loop (i - 1)
444-
// loop (source.Length - 1)
445-
446-
// let findLastIndex predicate (source: 'T[]) =
447-
// let rec loop i =
448-
// if i < 0 then -1
449-
// elif predicate source.[i] then i
450-
// else loop (i - 1)
451-
// loop (source.Length - 1)
452-
453-
// let findIndexBack predicate (source: 'T[]) =
454-
// let rec loop i =
455-
// if i < 0 then indexNotFound()
456-
// elif predicate source.[i] then i
457-
// else loop (i - 1)
458-
// loop (source.Length - 1)
459-
460-
// let tryFindIndexBack predicate (source: 'T[]) =
461-
// let rec loop i =
462-
// if i < 0 then None
463-
// elif predicate source.[i] then Some i
464-
// else loop (i - 1)
465-
// loop (source.Length - 1)
466-
467-
// let choose (chooser: 'T->'U option) (source: 'T[]) ([<Inject>] cons: Cons<'U>) =
468-
// let res: 'U[] = [||]
469-
// for i = 0 to source.Length - 1 do
470-
// match chooser source.[i] with
471-
// | None -> ()
472-
// | Some y -> pushImpl res y |> ignore
473-
// match box cons with
474-
// | null -> res // avoid extra copy
475-
// | _ -> map id res cons
399+
let tryFind (predicate: 'T -> bool) (source: 'T[]): 'T option =
400+
let rec loop i (source: 'T[]) =
401+
if i >= source.Length then None
402+
elif predicate source.[i] then Some source.[i]
403+
else loop (i + 1) source
404+
loop 0 source
405+
406+
let find (predicate: 'T -> bool) (source: 'T[]): 'T =
407+
match tryFind predicate source with
408+
| Some res -> res
409+
| None -> indexNotFound()
410+
411+
let tryFindIndex (predicate: 'T -> bool) (source: 'T[]): int option =
412+
let rec loop i (source: 'T[]) =
413+
if i >= source.Length then None
414+
elif predicate source.[i] then Some i
415+
else loop (i + 1) source
416+
loop 0 source
417+
418+
let findIndex (predicate: 'T -> bool) (source: 'T[]): int =
419+
match tryFindIndex predicate source with
420+
| Some res -> res
421+
| None -> indexNotFound()
422+
423+
let tryFindBack predicate (source: 'T[]) =
424+
let rec loop i (source: 'T[]) =
425+
if i < 0 then None
426+
elif predicate source.[i] then Some source.[i]
427+
else loop (i - 1) source
428+
loop (source.Length - 1) source
429+
430+
let findBack predicate (source: 'T[]) =
431+
match tryFindBack predicate source with
432+
| Some res -> res
433+
| None -> indexNotFound()
434+
435+
let tryFindIndexBack predicate (source: 'T[]) =
436+
let rec loop i (source: 'T[]) =
437+
if i < 0 then None
438+
elif predicate source.[i] then Some i
439+
else loop (i - 1) source
440+
loop (source.Length - 1) source
441+
442+
let findIndexBack predicate (source: 'T[]) =
443+
match tryFindIndexBack predicate source with
444+
| Some res -> res
445+
| None -> indexNotFound()
446+
447+
let findLastIndex predicate (source: 'T[]) =
448+
match tryFindIndexBack predicate source with
449+
| Some res -> res
450+
| None -> -1
451+
452+
let tryPick chooser (source: 'T[]) =
453+
let rec loop i (source: 'T[]) =
454+
if i >= source.Length then
455+
None
456+
else
457+
match chooser source.[i] with
458+
| None -> loop (i + 1) source
459+
| res -> res
460+
loop 0 source
461+
462+
let pick chooser (source: 'T[]) =
463+
match tryPick chooser source with
464+
| Some res -> res
465+
| None -> indexNotFound()
466+
467+
let choose (chooser: 'T -> 'U option) (source: 'T[]) = //([<Inject>] cons: Cons<'U>) =
468+
let res = ResizeArray<'U>()
469+
for i = 0 to source.Length - 1 do
470+
match chooser source.[i] with
471+
| None -> ()
472+
| Some y -> res.Add(y)
473+
res.ToArray()
476474

477475
let fold folder (state: 'State) (source: 'T[]) =
478476
let mutable acc = state
@@ -665,14 +663,14 @@ let iterateIndexed2 action (source1: 'T[]) (source2: 'T[]) =
665663
// // add each chunk to the res
666664
// for x = 0 to int(System.Math.Ceiling(float(source.Length) / float(chunkSize))) - 1 do
667665
// let start = x * chunkSize
668-
// let slice = subArrayImpl source start chunkSize
666+
// let slice = getSubArray source start chunkSize
669667
// pushImpl res slice |> ignore
670668
// res
671669

672670
// let splitAt (index: int) (source: 'T[]): 'T[] * 'T[] =
673671
// if index < 0 || index > source.Length then
674672
// invalidArg "index" SR.indexOutOfBounds
675-
// subArrayImpl array 0 index, skipImpl array index
673+
// getSubArray array 0 index, skipImpl array index
676674

677675
// let compareWith (comparer: 'T -> 'T -> int) (source1: 'T[]) (source2: 'T[]) =
678676
// if isNull source1 then
@@ -761,6 +759,9 @@ let reduceBack reduction (source: 'T[]) =
761759
// Redirected to List.ofArray to avoid dependency (see Replacements)
762760
// let toList (source: 'T[]) = List.ofArray
763761

762+
// Redirected to Seq.ofArray to avoid dependency (see Replacements)
763+
// let toSeq (source: 'T[]) = Seq.ofArray
764+
764765
// let windowed (windowSize: int) (source: 'T[]): 'T[][] =
765766
// if windowSize <= 0 then
766767
// failwith "windowSize must be positive"
@@ -782,7 +783,7 @@ let reduceBack reduction (source: 'T[]) =
782783
// for i = 0 to chunks - 1 do
783784
// let chunkSize = if i < chunksWithExtraItem then minChunkSize + 1 else minChunkSize
784785
// let start = i * minChunkSize + (FSharp.Core.Operators.min chunksWithExtraItem i)
785-
// let slice = subArrayImpl source start chunkSize
786+
// let slice = getSubArray source start chunkSize
786787
// pushImpl res slice |> ignore
787788
// res
788789

0 commit comments

Comments
 (0)