Skip to content

Commit a15788b

Browse files
fix
Fixing issue where FastBufferReader did not provide constructors to honor an ArraySegment's configuration.
1 parent 18aed1f commit a15788b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferReader.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,61 @@ public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocato
136136
}
137137
fixed (byte* data = buffer.Array)
138138
{
139+
139140
Handle = CreateHandle(data, length == -1 ? buffer.Count : length, offset, copyAllocator, Allocator.Temp);
140141
}
141142
}
142143

144+
/// <summary>
145+
/// Create a FastBufferReader from an ArraySegment that uses the ArraySegment.Offset for the reader's offset.
146+
///
147+
/// A new buffer will be created using the given allocator and the value will be copied in.
148+
/// FastBufferReader will then own the data.
149+
///
150+
/// Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block
151+
/// and ensure the FastBufferReader isn't used outside that block.
152+
/// </summary>
153+
/// <param name="buffer">The buffer to copy from</param>
154+
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
155+
/// <param name="length">The number of bytes to copy (all if this is -1)</param>
156+
/// <exception cref="NotSupportedException"></exception>
157+
public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator, int length = -1)
158+
{
159+
if (copyAllocator == Allocator.None)
160+
{
161+
throw new NotSupportedException("Allocator.None cannot be used with managed source buffers.");
162+
}
163+
fixed (byte* data = buffer.Array)
164+
{
165+
166+
Handle = CreateHandle(data, length == -1 ? buffer.Count : length, buffer.Offset, copyAllocator, Allocator.Temp);
167+
}
168+
}
169+
170+
/// <summary>
171+
/// Create a FastBufferReader from an ArraySegment that uses the ArraySegment.Offset for the reader's offset and the ArraySegment.Count for the reader's length.
172+
///
173+
/// A new buffer will be created using the given allocator and the value will be copied in.
174+
/// FastBufferReader will then own the data.
175+
///
176+
/// Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block
177+
/// and ensure the FastBufferReader isn't used outside that block.
178+
/// </summary>
179+
/// <param name="buffer">The buffer to copy from</param>
180+
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
181+
/// <exception cref="NotSupportedException"></exception>
182+
public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator)
183+
{
184+
if (copyAllocator == Allocator.None)
185+
{
186+
throw new NotSupportedException("Allocator.None cannot be used with managed source buffers.");
187+
}
188+
fixed (byte* data = buffer.Array)
189+
{
190+
Handle = CreateHandle(data, buffer.Count, buffer.Offset, copyAllocator, Allocator.Temp);
191+
}
192+
}
193+
143194
/// <summary>
144195
/// Create a FastBufferReader from an existing byte array.
145196
///

0 commit comments

Comments
 (0)