/// <summary> /// Copies the contents of this span into another. The destination /// must be at least as big as the source, and may be bigger. /// </summary> /// <param name="destination">The span to copy items into.</param> public bool TryCopyTo(Span <T> destination) { // There are some benefits of making local copies. See https://github.com/dotnet/coreclr/issues/5556 var dest = destination; var src = this; if (src.Length > dest.Length) { return(false); } if (default(T) != null && MemoryUtils.IsPrimitiveValueType <T>()) { PtrUtils.CopyBlock(src.Object, src.Offset, dest.Object, dest.Offset, src.Length * PtrUtils.SizeOf <T>()); } else { for (int i = 0; i < src.Length; i++) { // We don't check bounds here as we are surely within them T value = PtrUtils.Get <T>(src.Object, src.Offset, (UIntPtr)i); PtrUtils.Set(dest.Object, dest.Offset, (UIntPtr)i, value); } } return(true); }
/// <summary> /// Fetches the element at the specified index. /// </summary> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when the specified index is not in range (<0 or >&eq;length). /// </exception> public T this[int index] { get { Contract.RequiresInRange(index, Length); return(PtrUtils.Get <T>( m_object, m_offset + (index * PtrUtils.SizeOf <T>()))); } set { Contract.RequiresInRange(index, Length); PtrUtils.Set <T>( m_object, m_offset + (index * PtrUtils.SizeOf <T>()), value); } }
/// <summary> /// Fetches the element at the specified index. /// </summary> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when the specified index is not in range (<0 or >&eq;length). /// </exception> public T this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { Contract.RequiresInRange(index, (uint)Length); return(PtrUtils.Get <T>(Object, Offset, (UIntPtr)index)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private set { Contract.RequiresInRange(index, (uint)Length); PtrUtils.Set(Object, Offset, (UIntPtr)index, value); } }
/// <summary> /// Fetches the element at the specified index. /// </summary> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when the specified index is not in range (<0 or >&eq;length). /// </exception> public T this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { Contract.RequiresInRange(index, Length); return(PtrUtils.Get <T>( _object, _offset + (index * PtrUtils.SizeOf <T>()))); } [MethodImpl(MethodImplOptions.AggressiveInlining)] set { Contract.RequiresInRange(index, Length); PtrUtils.Set <T>( _object, _offset + (index * PtrUtils.SizeOf <T>()), value); } }
public static void Write <[Primitive] T>(this Span <byte> slice, T value) where T : struct { Contract.RequiresInInclusiveRange(PtrUtils.SizeOf <T>(), (uint)slice.Length); PtrUtils.Set(slice.Object, slice.Offset, (UIntPtr)0, value); }
public static void Write <[Primitive] T>(this Span <byte> slice, T value) where T : struct { Contract.Requires(slice.Length >= PtrUtils.SizeOf <T>()); PtrUtils.Set(slice.Object, slice.Offset, value); }