From 857e81518f271649f7ced13a46561eedf3ec9b5e Mon Sep 17 00:00:00 2001 From: Andrei Sergeev Date: Thu, 11 Jan 2024 10:13:44 +0400 Subject: [PATCH] Add Cast method --- .../FlatArray.T/Functional/Functional.Cast.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Cast.cs diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Cast.cs b/src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Cast.cs new file mode 100644 index 0000000..2922ad4 --- /dev/null +++ b/src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Cast.cs @@ -0,0 +1,29 @@ +namespace System; + +partial struct FlatArray +{ + // TODO: Add the tests and make public + internal FlatArray Cast() + { + if (length == default) + { + return default; + } + + if (items is TResult[] resultItems) + { + return new(length, resultItems); + } + + resultItems = new TResult[length]; + + var counter = 0; + do + { + resultItems[counter] = (TResult)(object)items![counter]!; + } + while (++counter < resultItems.Length); + + return new(resultItems, default); + } +}