From 5a0fc337aaf12b2ffa5d2c94b4569175cfe5ace8 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 25 Jun 2020 14:15:04 +1200 Subject: [PATCH] Clean up --- .../Google.Protobuf.Test/ByteStringTest.cs | 2 +- csharp/src/Google.Protobuf/ByteString.cs | 45 ++++++------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs index 18de7c4f5f08..a52993f932ce 100644 --- a/csharp/src/Google.Protobuf.Test/ByteStringTest.cs +++ b/csharp/src/Google.Protobuf.Test/ByteStringTest.cs @@ -181,7 +181,7 @@ public void GetEnumerator() public void UnsafeFromBytes() { byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6 }; - ByteString bs = ByteString.Unsafe.FromBytes(data.AsMemory(2, 3)); + ByteString bs = ByteString.UnsafeFromBytes(data.AsMemory(2, 3)); ReadOnlySpan s = bs.Span; Assert.AreEqual(3, s.Length); diff --git a/csharp/src/Google.Protobuf/ByteString.cs b/csharp/src/Google.Protobuf/ByteString.cs index ef719ab4b637..ad83da753fe6 100644 --- a/csharp/src/Google.Protobuf/ByteString.cs +++ b/csharp/src/Google.Protobuf/ByteString.cs @@ -53,7 +53,7 @@ namespace Google.Protobuf [SecuritySafeCritical] public sealed class ByteString : IEnumerable, IEquatable { - private static readonly ByteString empty; + private static readonly ByteString empty = new ByteString(new byte[0]); #if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY private readonly ReadOnlyMemory bytes; @@ -61,28 +61,6 @@ public sealed class ByteString : IEnumerable, IEquatable private readonly byte[] bytes; #endif - static ByteString() - { - empty = new ByteString(new byte[0]); - } - -#if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY - /// - /// Unsafe operations that can cause IO Failure and/or other catastrophic side-effects. - /// - public static class Unsafe - { - /// - /// Constructs a new ByteString from the given bytes. The bytes are - /// *not* copied, and must not be modified after this constructor is called. - /// - public static ByteString FromBytes(ReadOnlyMemory bytes) - { - return new ByteString(bytes); - } - } -#endif - /// /// Internal use only. Ensure that the provided array is not mutated and belongs to this instance. /// @@ -142,10 +120,7 @@ public bool IsEmpty /// public ReadOnlySpan Span { - get - { - return bytes.Span; - } + get { return bytes.Span; } } /// @@ -154,10 +129,7 @@ public ReadOnlySpan Span /// public ReadOnlyMemory Memory { - get - { - return bytes; - } + get { return bytes; } } #endif @@ -197,6 +169,17 @@ public string ToBase64() #endif } +#if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY + /// + /// Constructs a new from the given bytes. The bytes are not copied, + /// and must not be modified while the is in use. + /// + public static ByteString UnsafeFromBytes(ReadOnlyMemory bytes) + { + return new ByteString(bytes); + } +#endif + /// /// Constructs a from the Base64 Encoded String. ///