forked from jamescourtney/FlatSharp
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUnsafeOptions.cs
More file actions
65 lines (56 loc) · 2.09 KB
/
Copy pathUnsafeOptions.cs
File metadata and controls
65 lines (56 loc) · 2.09 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright 2021 James Courtney
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Samples.UnsafeOptions;
/// <summary>
/// This sample shows various unsafe optimizaitons that FlatSharp can make.
/// These unsafe operations are generally more performant than their safe alternatives,
/// but carry the general risks that Unsafe code does. These are "at your own risk"
/// options. While they are tested and validated in FlatSharp, you own ultimate
/// validation and testing if you choose to use these features.
/// </summary>
public class UnsafeOptionsExample : IFlatSharpSample
{
public bool HasConsoleOutput => false;
public void Run()
{
FourBytes four = new FourBytes { A = 4 };
SixteenBytes sixteen = new SixteenBytes();
for (int i = 0; i < sixteen.Data_Length; ++i)
{
sixteen.Data(i) = (byte)i;
}
Vector3 vec3 = new Vector3(1, 2, 3);
TwentyBytes twenty = new TwentyBytes { Four = four, Sixteen = sixteen };
RootTable table = new()
{
Union = new UnsafeUnion[]
{
new UnsafeUnion(four),
new UnsafeUnion(sixteen),
new UnsafeUnion(vec3),
new UnsafeUnion(twenty),
}
};
Assert.True(
Unsafe.SizeOf<UnsafeUnion>() == 21,
"The size of unsafe unions is that of their largest member plus 1.");
Assert.True(
Unsafe.SizeOf<TwentyBytes>() == 20,
"Size is what we expected!");
}
}