-
Notifications
You must be signed in to change notification settings - Fork 4k
/
ImmutableArrayInterop.cs
36 lines (31 loc) · 1.1 KB
/
ImmutableArrayInterop.cs
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Runtime.InteropServices;
namespace Roslyn.Utilities
{
internal static class ImmutableByteArrayInterop
{
internal static byte[] DangerousGetUnderlyingArray(this ImmutableArray<byte> array)
{
var union = new ByteArrayUnion();
union.ImmutableArray = array;
return union.MutableArray;
}
internal static ImmutableArray<byte> DangerousCreateFromUnderlyingArray(ref byte[] array)
{
var union = new ByteArrayUnion();
union.MutableArray = array;
array = null;
return union.ImmutableArray;
}
[StructLayout(LayoutKind.Explicit)]
private struct ByteArrayUnion
{
[FieldOffset(0)]
internal byte[] MutableArray;
[FieldOffset(0)]
internal ImmutableArray<byte> ImmutableArray;
}
}
}