Skip to content

Commit 0c85e16

Browse files
committed
add System.Memory dependency
also add useful Span-based methods for ByteString
1 parent b68a347 commit 0c85e16

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

csharp/src/Google.Protobuf/ByteString.cs

+22-11
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,6 @@ internal static ByteString FromBytes(byte[] bytes)
6767
{
6868
return new ByteString(bytes);
6969
}
70-
71-
/// <summary>
72-
/// Provides direct, unrestricted access to the bytes contained in this instance.
73-
/// You must not modify or resize the byte array returned by this method.
74-
/// </summary>
75-
internal static byte[] GetBuffer(ByteString bytes)
76-
{
77-
return bytes.bytes;
78-
}
7970
}
8071

8172
/// <summary>
@@ -119,6 +110,14 @@ public bool IsEmpty
119110
get { return Length == 0; }
120111
}
121112

113+
#if NETSTANDARD2_0
114+
/// <summary>
115+
/// Provides read-only access to the data of this <see cref="ByteString"/>.
116+
/// No data is copied so this is the most efficient way of accessing.
117+
/// </summary>
118+
public ReadOnlySpan<byte> Span => new ReadOnlySpan<byte>(bytes);
119+
#endif
120+
122121
/// <summary>
123122
/// Converts this <see cref="ByteString"/> into a byte array.
124123
/// </summary>
@@ -161,7 +160,7 @@ public static ByteString FromStream(Stream stream)
161160
int capacity = stream.CanSeek ? checked((int) (stream.Length - stream.Position)) : 0;
162161
var memoryStream = new MemoryStream(capacity);
163162
stream.CopyTo(memoryStream);
164-
#if NETSTANDARD1_0
163+
#if NETSTANDARD1_0 || NETSTANDARD2_0
165164
byte[] bytes = memoryStream.ToArray();
166165
#else
167166
// Avoid an extra copy if we can.
@@ -187,7 +186,7 @@ public static ByteString FromStream(Stream stream)
187186
// We have to specify the buffer size here, as there's no overload accepting the cancellation token
188187
// alone. But it's documented to use 81920 by default if not specified.
189188
await stream.CopyToAsync(memoryStream, 81920, cancellationToken);
190-
#if NETSTANDARD1_0
189+
#if NETSTANDARD1_0 || NETSTANDARD2_0
191190
byte[] bytes = memoryStream.ToArray();
192191
#else
193192
// Avoid an extra copy if we can.
@@ -219,6 +218,18 @@ public static ByteString CopyFrom(byte[] bytes, int offset, int count)
219218
return new ByteString(portion);
220219
}
221220

221+
#if NETSTANDARD2_0
222+
/// <summary>
223+
/// Constructs a <see cref="ByteString" /> from a read only span. The contents
224+
/// are copied, so further modifications to the span will not
225+
/// be reflected in the returned <see cref="ByteString" />.
226+
/// </summary>
227+
public static ByteString CopyFrom(ReadOnlySpan<byte> bytes)
228+
{
229+
return new ByteString(bytes.ToArray());
230+
}
231+
#endif
232+
222233
/// <summary>
223234
/// Creates a new <see cref="ByteString" /> by encoding the specified text with
224235
/// the given encoding.

csharp/src/Google.Protobuf/Google.Protobuf.csproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<VersionPrefix>3.7.0</VersionPrefix>
88
<LangVersion>6</LangVersion>
99
<Authors>Google Inc.</Authors>
10-
<TargetFrameworks>netstandard1.0;net45</TargetFrameworks>
10+
<TargetFrameworks>netstandard1.0;netstandard2.0;net45</TargetFrameworks>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1212
<AssemblyOriginatorKeyFile>../../keys/Google.Protobuf.snk</AssemblyOriginatorKeyFile>
1313
<SignAssembly>true</SignAssembly>
@@ -28,9 +28,13 @@
2828
- Visual Studio.
2929
-->
3030
<PropertyGroup Condition="'$(OS)' != 'Windows_NT'">
31-
<TargetFrameworks>netstandard1.0</TargetFrameworks>
31+
<TargetFrameworks>netstandard1.0;netstandard2.0</TargetFrameworks>
3232
</PropertyGroup>
3333

34+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
35+
<PackageReference Include="System.Memory" Version="4.5.2" />
36+
</ItemGroup>
37+
3438
<ItemGroup>
3539
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05" PrivateAssets="All" />
3640
</ItemGroup>

0 commit comments

Comments
 (0)