Add vector tuple deconstruction

This commit is contained in:
Oliver Booth 2022-05-16 10:30:55 +01:00
parent 1bb1feb89b
commit d312d05f7a
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
13 changed files with 165 additions and 0 deletions

View File

@ -1,5 +1,14 @@
# Changelog
## 3.2.0
### Added
- X10D: Added `Vector2.Deconstruct()`
- X10D: Added `Vector3.Deconstruct()`
- X10D: Added `Vector4.Deconstruct()`
- X10D.Unity: Added `Vector2.Deconstruct()`
- X10D.Unity: Added `Vector3.Deconstruct()`
- X10D.Unity: Added `Vector4.Deconstruct()`
## [3.1.0]
### Added
- Reintroduced Unity support

View File

@ -7,6 +7,16 @@ namespace X10D.Tests.Numerics;
[TestClass]
public class Vector2Tests
{
[TestMethod]
public void Deconstruct_ShouldReturnCorrectValues()
{
var vector = new Vector2(1, 2);
(float x, float y) = vector;
Assert.AreEqual(1, x);
Assert.AreEqual(2, y);
}
[TestMethod]
public void WithX_ShouldReturnVectorWithNewX_GivenVector()
{

View File

@ -7,6 +7,17 @@ namespace X10D.Tests.Numerics;
[TestClass]
public class Vector3Tests
{
[TestMethod]
public void Deconstruct_ShouldReturnCorrectValues()
{
var vector = new Vector3(1, 2, 3);
(float x, float y, float z) = vector;
Assert.AreEqual(1, x);
Assert.AreEqual(2, y);
Assert.AreEqual(3, z);
}
[TestMethod]
public void WithX_ShouldReturnVectorWithNewX_GivenVector()
{

View File

@ -7,6 +7,18 @@ namespace X10D.Tests.Numerics;
[TestClass]
public class Vector4Tests
{
[TestMethod]
public void Deconstruct_ShouldReturnCorrectValues()
{
var vector = new Vector4(1, 2, 3, 4);
(float x, float y, float z, float w) = vector;
Assert.AreEqual(1, x);
Assert.AreEqual(2, y);
Assert.AreEqual(3, z);
Assert.AreEqual(4, w);
}
[TestMethod]
public void WithW_ShouldReturnVectorWithNewW_GivenVector()
{

View File

@ -10,6 +10,18 @@ namespace X10D.Unity.Tests.Numerics
{
public class Vector2Tests
{
[UnityTest]
public IEnumerator Deconstruct_ShouldReturnCorrectValues()
{
var vector = new Vector2(1, 2);
(float x, float y) = vector;
Assert.AreEqual(1, x);
Assert.AreEqual(2, y);
yield break;
}
[UnityTest]
public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents()
{

View File

@ -10,6 +10,19 @@ namespace X10D.Unity.Tests.Numerics
{
public class Vector3Tests
{
[UnityTest]
public IEnumerator Deconstruct_ShouldReturnCorrectValues()
{
var vector = new Vector3(1, 2, 3);
(float x, float y, float z) = vector;
Assert.AreEqual(1, x);
Assert.AreEqual(2, y);
Assert.AreEqual(3, z);
yield break;
}
[UnityTest]
public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents()
{

View File

@ -10,6 +10,20 @@ namespace X10D.Unity.Tests.Numerics
{
public class Vector4Tests
{
[UnityTest]
public IEnumerator Deconstruct_ShouldReturnCorrectValues()
{
var vector = new Vector4(1, 2, 3, 4);
(float x, float y, float z, float w) = vector;
Assert.AreEqual(1, x);
Assert.AreEqual(2, y);
Assert.AreEqual(3, z);
Assert.AreEqual(4, w);
yield break;
}
[UnityTest]
public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents()
{

View File

@ -9,6 +9,18 @@ namespace X10D.Unity.Numerics;
/// </summary>
public static class Vector2Extensions
{
/// <summary>
/// Deconstructs the current <see cref="Vector2" /> into its components.
/// </summary>
/// <param name="vector">The vector to deconstruct.</param>
/// <param name="x">The X component value.</param>
/// <param name="y">The Y component value.</param>
public static void Deconstruct(this Vector2 vector, out float x, out float y)
{
x = vector.x;
y = vector.y;
}
/// <summary>
/// Converts the current vector to a <see cref="System.Numerics.Vector2" />.
/// </summary>

View File

@ -9,6 +9,20 @@ namespace X10D.Unity.Numerics;
/// </summary>
public static class Vector3Extensions
{
/// <summary>
/// Deconstructs the current <see cref="Vector3" /> into its components.
/// </summary>
/// <param name="vector">The vector to deconstruct.</param>
/// <param name="x">The X component value.</param>
/// <param name="y">The Y component value.</param>
/// <param name="z">The Z component value.</param>
public static void Deconstruct(this Vector3 vector, out float x, out float y, out float z)
{
x = vector.x;
y = vector.y;
z = vector.z;
}
/// <summary>
/// Converts the current vector to a <see cref="System.Numerics.Vector3" />.
/// </summary>

View File

@ -9,6 +9,22 @@ namespace X10D.Unity.Numerics;
/// </summary>
public static class Vector4Extensions
{
/// <summary>
/// Deconstructs the current <see cref="Vector4" /> into its components.
/// </summary>
/// <param name="vector">The vector to deconstruct.</param>
/// <param name="x">The X component value.</param>
/// <param name="y">The Y component value.</param>
/// <param name="z">The Z component value.</param>
/// <param name="w">The W component value.</param>
public static void Deconstruct(this Vector4 vector, out float x, out float y, out float z, out float w)
{
x = vector.x;
y = vector.y;
z = vector.z;
w = vector.w;
}
/// <summary>
/// Converts the current vector to a <see cref="System.Numerics.Vector4" />.
/// </summary>

View File

@ -9,6 +9,18 @@ namespace X10D.Numerics;
/// </summary>
public static class Vector2Extensions
{
/// <summary>
/// Deconstructs the current <see cref="Vector2" /> into its components.
/// </summary>
/// <param name="vector">The vector to deconstruct.</param>
/// <param name="x">The X component value.</param>
/// <param name="y">The Y component value.</param>
public static void Deconstruct(this Vector2 vector, out float x, out float y)
{
x = vector.X;
y = vector.Y;
}
/// <summary>
/// Returns a vector whose Y component is the same as the specified vector, and whose X component is a new value.
/// </summary>

View File

@ -9,6 +9,20 @@ namespace X10D.Numerics;
/// </summary>
public static class Vector3Extensions
{
/// <summary>
/// Deconstructs the current <see cref="Vector3" /> into its components.
/// </summary>
/// <param name="vector">The vector to deconstruct.</param>
/// <param name="x">The X component value.</param>
/// <param name="y">The Y component value.</param>
/// <param name="z">The Z component value.</param>
public static void Deconstruct(this Vector3 vector, out float x, out float y, out float z)
{
x = vector.X;
y = vector.Y;
z = vector.Z;
}
/// <summary>
/// Returns a vector whose Y and Z components are the same as the specified vector, and whose X component is a new value.
/// </summary>

View File

@ -9,6 +9,22 @@ namespace X10D.Numerics;
/// </summary>
public static class Vector4Extensions
{
/// <summary>
/// Deconstructs the current <see cref="Vector4" /> into its components.
/// </summary>
/// <param name="vector">The vector to deconstruct.</param>
/// <param name="x">The X component value.</param>
/// <param name="y">The Y component value.</param>
/// <param name="z">The Z component value.</param>
/// <param name="w">The W component value.</param>
public static void Deconstruct(this Vector4 vector, out float x, out float y, out float z, out float w)
{
x = vector.X;
y = vector.Y;
z = vector.Z;
w = vector.W;
}
/// <summary>
/// Returns a vector whose Y, Z, and W components are the same as the specified vector, and whose X component is a new
/// value.