Browse Source

Minor optimizations, improve XML docs and annotations

js/color-alpha-handling
Sergio Pedri 6 years ago
parent
commit
5397ab328f
  1. 37
      src/ImageSharp/Memory/MemoryOwnerExtensions.cs

37
src/ImageSharp/Memory/MemoryOwnerExtensions.cs

@ -3,6 +3,7 @@
using System; using System;
using System.Buffers; using System.Buffers;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -13,12 +14,29 @@ namespace SixLabors.ImageSharp.Memory
/// </summary> /// </summary>
internal static class MemoryOwnerExtensions internal static class MemoryOwnerExtensions
{ {
/// <summary>
/// Gets a <see cref="Span{T}"/> from an <see cref="IMemoryOwner{T}"/> instance.
/// </summary>
/// <param name="buffer">The buffer</param>
/// <returns>The <see cref="Span{T}"/></returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> GetSpan<T>(this IMemoryOwner<T> buffer) public static Span<T> GetSpan<T>(this IMemoryOwner<T> buffer)
=> buffer.Memory.Span; {
return buffer.Memory.Span;
}
/// <summary>
/// Gets the length of an <see cref="IMemoryOwner{T}"/> internal buffer.
/// </summary>
/// <param name="buffer">The buffer</param>
/// <returns>The length of the buffer</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Length<T>(this IMemoryOwner<T> buffer) public static int Length<T>(this IMemoryOwner<T> buffer)
=> buffer.GetSpan().Length; {
return buffer.Memory.Length;
}
/// <summary> /// <summary>
/// Gets a <see cref="Span{T}"/> to an offsetted position inside the buffer. /// Gets a <see cref="Span{T}"/> to an offsetted position inside the buffer.
@ -26,6 +44,7 @@ namespace SixLabors.ImageSharp.Memory
/// <param name="buffer">The buffer</param> /// <param name="buffer">The buffer</param>
/// <param name="start">The start</param> /// <param name="start">The start</param>
/// <returns>The <see cref="Span{T}"/></returns> /// <returns>The <see cref="Span{T}"/></returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> Slice<T>(this IMemoryOwner<T> buffer, int start) public static Span<T> Slice<T>(this IMemoryOwner<T> buffer, int start)
{ {
@ -39,6 +58,7 @@ namespace SixLabors.ImageSharp.Memory
/// <param name="start">The start</param> /// <param name="start">The start</param>
/// <param name="length">The length of the slice</param> /// <param name="length">The length of the slice</param>
/// <returns>The <see cref="Span{T}"/></returns> /// <returns>The <see cref="Span{T}"/></returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> Slice<T>(this IMemoryOwner<T> buffer, int start, int length) public static Span<T> Slice<T>(this IMemoryOwner<T> buffer, int start, int length)
{ {
@ -55,8 +75,17 @@ namespace SixLabors.ImageSharp.Memory
buffer.GetSpan().Clear(); buffer.GetSpan().Clear();
} }
/// <summary>
/// Gets a reference to the first item in the internal buffer for an <see cref="IMemoryOwner{T}"/> instance.
/// </summary>
/// <param name="buffer">The buffer</param>
/// <returns>A reference to the first item within the memory wrapped by <paramref name="buffer"/></returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetReference<T>(this IMemoryOwner<T> buffer) public static ref T GetReference<T>(this IMemoryOwner<T> buffer)
where T : struct => where T : struct
ref MemoryMarshal.GetReference(buffer.GetSpan()); {
return ref MemoryMarshal.GetReference(buffer.GetSpan());
}
} }
} }

Loading…
Cancel
Save