• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-dotnet / 5716220196

31 Jul 2023 02:52PM UTC coverage: 82.478% (-0.3%) from 82.741%
5716220196

Pull #3261

github

aed2eb
fealebenpae
Merge remote-tracking branch 'origin/main' into yg/updated-marshaling

# Conflicts:
#	Realm/Realm/Handles/SharedRealmHandle.cs
Pull Request #3261: Use modern-er marshaling techniques

2029 of 2601 branches covered (78.01%)

Branch coverage included in aggregate %.

201 of 201 new or added lines in 21 files covered. (100.0%)

6246 of 7432 relevant lines covered (84.04%)

34048.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.0
/Realm/Realm/Native/MarshaledVector.cs
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2016 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
using System;
20
using System.Collections.Generic;
21
using System.Diagnostics;
22
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
24
using Realms.Native;
25

26
namespace Realms
27
{
28
    [StructLayout(LayoutKind.Sequential)]
29
    internal unsafe readonly struct MarshaledVector<T>
30
        where T : unmanaged
31
    {
32
        private readonly T* items;
33

34
        public readonly nint Count;
35

36
        public MarshaledVector(T* items, nint count)
37
        {
38
            this.items = items;
288,770✔
39
            Count = count;
288,770✔
40
        }
288,770✔
41

42
        public ref readonly T this[nint index]
43
        {
44
            get
45
            {
46
                if (index >= Count)
12,990!
47
                {
48
                    throw new ArgumentOutOfRangeException(nameof(index));
×
49
                }
50

51
                return ref Unsafe.Add(ref *items, index);
12,990✔
52
            }
53
        }
54

55
        public ref struct Enumerator
56
        {
57
            private readonly MarshaledVector<T> _vector;
58
            private nint _index;
59

60
            public Enumerator(MarshaledVector<T> vector)
61
            {
62
                _vector = vector;
3,713✔
63
                _index = -1;
3,713✔
64
            }
3,713✔
65

66
            public ref readonly T Current => ref _vector[_index];
10,769✔
67

68
            public bool MoveNext()
69
            {
70
                var index = _index + 1;
14,482✔
71
                if (index < _vector.Count)
14,482✔
72
                {
73
                    _index = index;
10,769✔
74
                    return true;
10,769✔
75
                }
76

77
                return false;
3,713✔
78
            }
79
        }
80

81
        public Enumerator GetEnumerator()
82
        {
83
            return new Enumerator(this);
3,713✔
84
        }
85

86
        public IEnumerable<T> ToEnumerable()
87
        {
88
            for (nint index = 0; index < Count; index++)
16,306✔
89
            {
90
                yield return this[index];
2,221✔
91
            }
92
        }
5,932✔
93

94
        public T[] ToArray()
95
        {
96
            var ret = new T[Count];
×
97
            fixed(T* destination = ret)
×
98
            {
99
                var byteSize = sizeof(T) * Count;
×
100
                Buffer.MemoryCopy(items, destination, byteSize, byteSize);
×
101
            }
102

103
            return ret;
×
104
        }
105

106
        public static MarshaledVector<T> AllocateEmpty(int capacity, Arena arena)
107
        {
108
            var buffer = arena.Allocate<T>(capacity);
×
109
            Unsafe.InitBlock(buffer.Data, 0, (uint)(sizeof(T) * capacity));
×
110
            return new MarshaledVector<T>(buffer.Data, capacity);
×
111
        }
112

113
        public static unsafe MarshaledVector<T> AllocateFrom(IReadOnlyCollection<T>? collection, Arena arena)
114
        {
115
            if (collection == null || collection.Count == 0)
288,770✔
116
            {
117
                return new MarshaledVector<T>(null, 0);
3,608✔
118
            }
119

120
            var buffer = arena.Allocate<T>(collection.Count);
285,162✔
121
            var i = 0;
285,162✔
122
            foreach (var item in collection)
4,234,090✔
123
            {
124
                buffer.Data[i++] = item;
1,831,883✔
125
            }
126

127
            return new MarshaledVector<T>(buffer.Data, buffer.Length);
285,162✔
128
        }
129
    }
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc