forked from UbiquityDotNET/Ubiquity.NET.Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugAssert.cs
More file actions
30 lines (28 loc) · 1.57 KB
/
Copy pathDebugAssert.cs
File metadata and controls
30 lines (28 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (c) Ubiquity.NET Contributors. All rights reserved.
// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information.
using System.Diagnostics;
namespace Ubiquity.NET.CodeAnalysis.Utils
{
/// <summary>Utility class to support Debug asserts</summary>
public static class DebugAssert
{
/// <summary>Tests if a structure size is < 16 bytes and generates a debug assertion if not</summary>
/// <typeparam name="T">Type of the struct to test</typeparam>
/// <remarks>
/// <para>This uses a runtime debug assert as it isn't possible to know the size at compile time of a managed struct.
/// The `sizeof` doesn't apply for anything with a managed reference or a native pointer sized member
/// as such sizes depend on the actual runtime used.</para>
/// <note type="important">
/// This function ONLY operates in a debug build. That is, the compiler will elide calls to this method
/// at the <em><b>call site</b></em> unless the "DEBUG" symbol is defined as it has a <see cref="ConditionalAttribute"/> attached to it.
/// </note>
/// </remarks>
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/choosing-between-class-and-struct"/>
[Conditional( "DEBUG" )]
public static void StructSizeOK<T>( )
where T : struct
{
Debug.Assert( Unsafe.SizeOf<T>() <= 16, $"{nameof( T )} size is > 16 bytes; Make it a class" );
}
}
}