From 655c8b72a590d2b3f0d3aab04a659f646f049867 Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Mon, 13 Nov 2017 21:11:53 -0800 Subject: [PATCH] Adding Memory CopyTo APIs (#15010) --- src/mscorlib/shared/System/Memory.cs | 23 +++++++++++++++++++++++ src/mscorlib/shared/System/ReadOnlyMemory.cs | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index c00de30..d83bd58 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -199,6 +199,29 @@ namespace System } } + /// + /// Copies the contents of the memory into the destination. If the source + /// and destination overlap, this method behaves as if the original values are in + /// a temporary location before the destination is overwritten. + /// + /// The Memory to copy items into. + /// + /// Thrown when the destination is shorter than the source. + /// + /// + public void CopyTo(Memory destination) => Span.CopyTo(destination.Span); + + /// + /// Copies the contents of the memory into the destination. If the source + /// and destination overlap, this method behaves as if the original values are in + /// a temporary location before the destination is overwritten. + /// + /// If the destination is shorter than the source, this method + /// return false and no data is written to the destination. + /// + /// The span to copy items into. + public bool TryCopyTo(Memory destination) => Span.TryCopyTo(destination.Span); + public unsafe MemoryHandle Retain(bool pin = false) { MemoryHandle memoryHandle; diff --git a/src/mscorlib/shared/System/ReadOnlyMemory.cs b/src/mscorlib/shared/System/ReadOnlyMemory.cs index 5240a37..1b163dd 100644 --- a/src/mscorlib/shared/System/ReadOnlyMemory.cs +++ b/src/mscorlib/shared/System/ReadOnlyMemory.cs @@ -176,6 +176,29 @@ namespace System } } + /// + /// Copies the contents of the read-only memory into the destination. If the source + /// and destination overlap, this method behaves as if the original values are in + /// a temporary location before the destination is overwritten. + /// + /// The Memory to copy items into. + /// + /// Thrown when the destination is shorter than the source. + /// + /// + public void CopyTo(Memory destination) => Span.CopyTo(destination.Span); + + /// + /// Copies the contents of the readonly-only memory into the destination. If the source + /// and destination overlap, this method behaves as if the original values are in + /// a temporary location before the destination is overwritten. + /// + /// If the destination is shorter than the source, this method + /// return false and no data is written to the destination. + /// + /// The span to copy items into. + public bool TryCopyTo(Memory destination) => Span.TryCopyTo(destination.Span); + /// Creates a handle for the memory. /// /// If pin is true, the GC will not move the array until the returned -- 2.7.4