add SafeMarshalCopy

This commit is contained in:
ousttrue
2021-12-22 15:24:32 +09:00
parent 345211bfe8
commit fa821ec603
10 changed files with 110 additions and 67 deletions

View File

@@ -89,15 +89,6 @@ namespace UniGLTF
public static class ArrayExtensions
{
public static int MarshalCopyTo<T>(this ArraySegment<byte> src, T[] dst) where T : struct
{
var size = dst.Length * Marshal.SizeOf(typeof(T));
using (var pin = Pin.Create(dst))
{
Marshal.Copy(src.Array, src.Offset, pin.Ptr, size);
}
return size;
}
public static T[] SelectInplace<T>(this T[] src, Func<T, T> pred)
{
@@ -107,21 +98,6 @@ namespace UniGLTF
}
return src;
}
public static void Copy<TFrom, TTo>(ArraySegment<TFrom> src, ArraySegment<TTo> dst)
where TFrom : struct
where TTo : struct
{
var bytes = new byte[src.Count * Marshal.SizeOf(typeof(TFrom))];
using (var pin = Pin.Create(src))
{
Marshal.Copy(pin.Ptr, bytes, 0, bytes.Length);
};
using (var pin = Pin.Create(dst))
{
Marshal.Copy(bytes, 0, pin.Ptr, bytes.Length);
};
}
}
public static class ListExtensions
@@ -134,7 +110,7 @@ namespace UniGLTF
}
public static class ArraySegmentExtensions
{
{
public static ArraySegment<T> Slice<T>(this ArraySegment<T> self, int start, int length)
{
if (start + length > self.Count)
@@ -156,6 +132,6 @@ namespace UniGLTF
}
return self.Slice(start, self.Count - start);
}
}
}