Skip to content

Commit

Permalink
Register numerics Vector Matrix Quaternion and Plane as deep immutables
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmikkers committed Oct 23, 2024
1 parent 76b2332 commit c509f4f
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions deepcopy/DeepCopyObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ private class DeepCopyContext
{
private static readonly Func<object, object> _shallowClone;

// Set of deeply immutable types. This includes all primitives, some known immutable valuetypes,
// and a few sealed immutable reference types such as 'string', 'DBNull' and 'Version'.
// Nullable<T> of an immutable valuetype T is itself immutable as well but rather than duplicating all those entries here,
// they are added programmatically in the static constructor below.
// Set of deeply immutable types. This includes all primitives, some known immutable
// valuetypes, and a few sealed immutable reference types such as 'string', 'DBNull' and
// 'Version'. Nullable<T> of an immutable valuetype T is itself immutable as well but rather
// than duplicating all those entries here, they are added programmatically in the static
// constructor below.
private static readonly HashSet<Type> _immutableTypes = new()
{
typeof(nint),
Expand All @@ -40,8 +41,15 @@ private class DeepCopyContext
typeof(double),
typeof(Half),
typeof(decimal),
typeof(Complex),
typeof(BigInteger),
typeof(Vector2),
typeof(Vector3),
typeof(Vector4),
typeof(Matrix3x2),
typeof(Matrix4x4),
typeof(Complex),
typeof(Quaternion),
typeof(Plane),
typeof(Guid),
typeof(DateTime),
typeof(DateOnly),
Expand Down Expand Up @@ -79,14 +87,9 @@ private static bool IsDeeplyImmutable(Type type)
{
// now that all primitives are included in the _immutableTypes set, the 'type.IsPrimitive' test is not really
// necessary, but I'll leave it in because it's a tiny bit faster than looking up items in the hashset.
if(type.IsPrimitive || type.IsEnum)
{
return true;
}
else
{
return _immutableTypes.Contains(type);
}
return type.IsPrimitive
|| type.IsEnum
|| _immutableTypes.Contains(type);
}

public object? InternalCopy(object? originalObject, bool includeInObjectGraph)
Expand Down

0 comments on commit c509f4f

Please sign in to comment.