-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to CommunityToolkit 8.2.2 and added Ref and ReadOnlyRef for .…
…NetStandard2.1
- Loading branch information
Showing
6 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using CommunityToolkit.HighPerformance.Helpers; | ||
|
||
namespace Arch.Core.Utils; | ||
|
||
#if NETSTANDARD2_1_OR_GREATER || NET6_0 | ||
|
||
/// <summary> | ||
/// The <see langword="struct"/> struct | ||
/// can store a readonly reference to a value of a specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The type of value to reference.</typeparam> | ||
public readonly ref struct ReadOnlyRef<T> | ||
{ | ||
/// <summary> | ||
/// The 1-length <see cref="ReadOnlySpan{T}"/> instance used to track the target <typeparamref name="T"/> value. | ||
/// </summary> | ||
internal readonly ReadOnlySpan<T> Span; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ReadOnlyRef{T}"/> struct. | ||
/// </summary> | ||
/// <param name="value">The readonly reference to the target <typeparamref name="T"/> value.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public ReadOnlyRef(in T value) | ||
{ | ||
ref T r0 = ref Unsafe.AsRef(value); | ||
|
||
this.Span = MemoryMarshal.CreateReadOnlySpan(ref r0, 1); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ReadOnlyRef{T}"/> struct. | ||
/// </summary> | ||
/// <param name="pointer">The pointer to the target value.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public unsafe ReadOnlyRef(void* pointer) | ||
: this(in Unsafe.AsRef<T>(pointer)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the readonly <typeparamref name="T"/> reference represented by the current <see cref="Ref{T}"/> instance. | ||
/// </summary> | ||
public ref readonly T Value | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => ref MemoryMarshal.GetReference(this.Span); | ||
} | ||
|
||
/// <summary> | ||
/// Implicitly converts a <see cref="Ref{T}"/> instance into a <see cref="ReadOnlyRef{T}"/> one. | ||
/// </summary> | ||
/// <param name="reference">The input <see cref="Ref{T}"/> instance.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator ReadOnlyRef<T>(Ref<T> reference) | ||
{ | ||
return new(in reference.Value); | ||
} | ||
|
||
/// <summary> | ||
/// Implicitly gets the <typeparamref name="T"/> value from a given <see cref="ReadOnlyRef{T}"/> instance. | ||
/// </summary> | ||
/// <param name="reference">The input <see cref="ReadOnlyRef{T}"/> instance.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator T(ReadOnlyRef<T> reference) | ||
{ | ||
return reference.Value; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace Arch.Core.Utils; | ||
|
||
#if NETSTANDARD2_1_OR_GREATER || NET6_0 | ||
|
||
/// <summary> | ||
/// The <see langword="struct"/> strct | ||
/// can store a reference to a value of a specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The type of value to reference.</typeparam> | ||
public readonly ref struct Ref<T> | ||
{ | ||
/// <summary> | ||
/// The 1-length <see cref="Span{T}"/> instance used to track the target <typeparamref name="T"/> value. | ||
/// </summary> | ||
internal readonly Span<T> Span; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Ref{T}"/> struct. | ||
/// </summary> | ||
/// <param name="value">The reference to the target <typeparamref name="T"/> value.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Ref(ref T value) | ||
{ | ||
this.Span = MemoryMarshal.CreateSpan(ref value, 1); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Ref{T}"/> struct. | ||
/// </summary> | ||
/// <param name="pointer">The pointer to the target value.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public unsafe Ref(void* pointer) | ||
: this(ref Unsafe.AsRef<T>(pointer)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <typeparamref name="T"/> reference represented by the current <see cref="Ref{T}"/> instance. | ||
/// </summary> | ||
public ref T Value | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => ref MemoryMarshal.GetReference(this.Span); | ||
} | ||
|
||
/// <summary> | ||
/// Implicitly gets the <typeparamref name="T"/> value from a given <see cref="Ref{T}"/> instance. | ||
/// </summary> | ||
/// <param name="reference">The input <see cref="Ref{T}"/> instance.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator T(Ref<T> reference) | ||
{ | ||
return reference.Value; | ||
} | ||
} | ||
#endif |