Skip to content

Commit 7721810

Browse files
committed
Make struct members readonly
1 parent f66f16b commit 7721810

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

src/NetMQ/Msg.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,25 @@ public struct Msg
119119
/// <summary>
120120
/// Returns true if msg is empty
121121
/// </summary>
122-
public bool IsEmpty => m_data == null || Size == 0;
122+
public readonly bool IsEmpty => m_data == null || Size == 0;
123123

124124
/// <summary>
125125
/// Returns true if the msg is join message
126126
/// </summary>
127-
public bool IsJoin => MsgType == MsgType.Join;
127+
public readonly bool IsJoin => MsgType == MsgType.Join;
128128

129129
/// <summary>
130130
/// Returns true if the msg is leave message
131131
/// </summary>
132-
public bool IsLeave => MsgType == MsgType.Leave;
132+
public readonly bool IsLeave => MsgType == MsgType.Leave;
133133

134134
/// <summary>
135135
/// Gets the position of the first element in the Data property delimited by the message,
136136
/// relative to the start of the original array.
137137
/// Deprecated: use <see cref="Slice()"/> or implicit casting to Span
138138
/// </summary>
139139
[Obsolete("Use implicit casting to Span or Slice instead")]
140-
public int Offset => m_offset;
140+
public readonly int Offset => m_offset;
141141

142142
#region MsgType
143143

@@ -149,14 +149,14 @@ public struct Msg
149149
/// which would indicate that this message is intended for use simply to mark a boundary
150150
/// between other parts of some unit of communication.
151151
/// </summary>
152-
public bool IsDelimiter => MsgType == MsgType.Delimiter;
152+
public readonly bool IsDelimiter => MsgType == MsgType.Delimiter;
153153

154154
/// <summary>Get whether this <see cref="Msg"/> is initialised and ready for use.</summary>
155155
/// <remarks>A newly constructed <see cref="Msg"/> is uninitialised, and can be initialised via one
156156
/// of <see cref="InitEmpty"/>, <see cref="InitDelimiter"/>, <see cref="InitGC(byte[],int)"/>, <see cref="InitGC(byte[],int,int)"/>, or <see cref="InitPool"/>.
157157
/// Calling <see cref="Close"/> will cause the <see cref="Msg"/> to become uninitialised again.</remarks>
158158
/// <returns><c>true</c> if the <see cref="Msg"/> is initialised, otherwise <c>false</c>.</returns>
159-
public bool IsInitialised => MsgType != MsgType.Uninitialised;
159+
public readonly bool IsInitialised => MsgType != MsgType.Uninitialised;
160160

161161
#endregion
162162

@@ -170,20 +170,20 @@ public struct Msg
170170
/// <summary>
171171
/// Get the "Has-More" flag, which when set on a message-queue frame indicates that there are more frames to follow.
172172
/// </summary>
173-
public bool HasMore => (Flags & MsgFlags.More) == MsgFlags.More;
173+
public readonly bool HasMore => (Flags & MsgFlags.More) == MsgFlags.More;
174174

175-
internal bool HasCommand => (Flags & MsgFlags.Command) == MsgFlags.Command;
175+
internal readonly bool HasCommand => (Flags & MsgFlags.Command) == MsgFlags.Command;
176176

177177
/// <summary>
178178
/// Get whether the <see cref="Data"/> buffer of this <see cref="Msg"/> is shared with another instance.
179179
/// Only applies to pooled message types.
180180
/// </summary>
181-
public bool IsShared => (Flags & MsgFlags.Shared) != 0;
181+
public readonly bool IsShared => (Flags & MsgFlags.Shared) != 0;
182182

183183
/// <summary>
184184
/// Get whether the Identity bit is set on the Flags property.
185185
/// </summary>
186-
public bool IsIdentity => (Flags & MsgFlags.Identity) != 0;
186+
public readonly bool IsIdentity => (Flags & MsgFlags.Identity) != 0;
187187

188188
/// <summary>
189189
/// Set the indicated Flags bits.
@@ -210,7 +210,7 @@ public void ResetFlags(MsgFlags flags)
210210
/// </summary>
211211
public uint RoutingId
212212
{
213-
get => m_routingId;
213+
readonly get => m_routingId;
214214
set
215215
{
216216
if (value == 0)
@@ -233,7 +233,7 @@ internal void ResetRoutingId()
233233
/// <exception cref="InvalidException">Value is larger than 255.</exception>
234234
public string Group
235235
{
236-
get => m_group;
236+
readonly get => m_group;
237237
set
238238
{
239239
if (value.Length > MaxGroupLength)
@@ -252,13 +252,13 @@ public string Group
252252
/// <see cref="NetMQ.MsgType.Empty"/> or <see cref="NetMQ.MsgType.Delimiter"/>.
253253
/// </remarks>
254254
[Obsolete("Use implicit casting to Span or Slice instead")]
255-
public byte[]? Data => m_data;
255+
public readonly byte[]? Data => m_data;
256256

257257
/// <summary>
258258
/// Return the internal buffer as Span
259259
/// </summary>
260260
/// <returns>The span</returns>
261-
public Span<byte> Slice()
261+
public readonly Span<byte> Slice()
262262
{
263263
if (m_data == null)
264264
return Span<byte>.Empty;
@@ -270,7 +270,7 @@ public Span<byte> Slice()
270270
/// Return the internal buffer as Memory
271271
/// </summary>
272272
/// <returns>The memory</returns>
273-
public Memory<byte> SliceAsMemory()
273+
public readonly Memory<byte> SliceAsMemory()
274274
{
275275
if (m_data == null)
276276
return Memory<byte>.Empty;
@@ -282,7 +282,7 @@ public Memory<byte> SliceAsMemory()
282282
/// Returns a slice of the internal buffer.
283283
/// </summary>
284284
/// <param name="offset">The offset to take the span from</param>
285-
public Span<byte> Slice(int offset)
285+
public readonly Span<byte> Slice(int offset)
286286
{
287287
if (m_data == null && offset > 0)
288288
throw new ArgumentOutOfRangeException(nameof(offset));
@@ -298,7 +298,7 @@ public Span<byte> Slice(int offset)
298298
/// </summary>
299299
/// <param name="offset">The offset to take the span from</param>
300300
/// <param name="count">The size of the slice</param>
301-
public Span<byte> Slice(int offset, int count)
301+
public readonly Span<byte> Slice(int offset, int count)
302302
{
303303
if (m_data == null && offset > 0)
304304
throw new ArgumentOutOfRangeException(nameof(offset));
@@ -316,7 +316,7 @@ public Span<byte> Slice(int offset, int count)
316316
/// Copy the content of the message into a Span
317317
/// </summary>
318318
/// <param name="span">The span to copy content to</param>
319-
public void CopyTo(Span<byte> span)
319+
public readonly void CopyTo(Span<byte> span)
320320
{
321321
((Span<byte>) this).CopyTo(span);
322322
}
@@ -325,7 +325,7 @@ public void CopyTo(Span<byte> span)
325325
/// Return a copy of the internal buffer as byte array
326326
/// </summary>
327327
/// <returns>Byte array</returns>
328-
public byte[] ToArray()
328+
public readonly byte[] ToArray()
329329
{
330330
var data = new byte[Size];
331331

@@ -357,7 +357,7 @@ public static implicit operator ReadOnlySpan<byte>(Msg msg)
357357
/// Returns span enumerator, to iterate of the Msg
358358
/// </summary>
359359
/// <returns>Span Enumerator</returns>
360-
public Span<byte>.Enumerator GetEnumerator()
360+
public readonly Span<byte>.Enumerator GetEnumerator()
361361
{
362362
return ((Span<byte>) this).GetEnumerator();
363363
}
@@ -536,7 +536,7 @@ public void RemoveReferences(int amount)
536536
/// Override the Object ToString method to show the object-type, and values of the MsgType, Size, and Flags properties.
537537
/// </summary>
538538
/// <returns>a string that provides some detail about this Msg's state</returns>
539-
public override string ToString()
539+
public override readonly string ToString()
540540
{
541541
return base.ToString() + "[" + MsgType + "," + Size + "," + Flags + "]";
542542
}
@@ -546,7 +546,7 @@ public override string ToString()
546546
/// </summary>
547547
/// <param name="encoding">The encoding to use for the conversion</param>
548548
/// <returns>The string</returns>
549-
public string GetString(Encoding encoding)
549+
public readonly string GetString(Encoding encoding)
550550
{
551551
Assumes.NotNull(m_data);
552552
return encoding.GetString(m_data, m_offset, Size);
@@ -559,7 +559,7 @@ public string GetString(Encoding encoding)
559559
/// <param name="offset">Offset to start conversion from</param>
560560
/// <param name="count">Number of bytes to convert</param>
561561
/// <returns>The string</returns>
562-
public string GetString(Encoding encoding, int offset, int count)
562+
public readonly string GetString(Encoding encoding, int offset, int count)
563563
{
564564
Assumes.NotNull(m_data);
565565
return encoding.GetString(m_data, m_offset + offset, count);
@@ -571,7 +571,7 @@ public string GetString(Encoding encoding, int offset, int count)
571571
/// <param name="src">the source byte-array to copy from</param>
572572
/// <param name="dstOffset">index within the internal Data array to copy that byte to</param>
573573
/// <param name="len">the number of bytes to copy</param>
574-
public void Put(byte[]? src, int dstOffset, int len)
574+
public readonly void Put(byte[]? src, int dstOffset, int len)
575575
{
576576
if (len == 0 || src == null)
577577
return;
@@ -586,7 +586,7 @@ public void Put(byte[]? src, int dstOffset, int len)
586586
/// <param name="srcOffset">first byte in the source byte-array</param>
587587
/// <param name="dstOffset">index within the internal Data array to copy that byte to</param>
588588
/// <param name="len">the number of bytes to copy</param>
589-
public void Put(byte[]? src, int srcOffset, int dstOffset, int len) {
589+
public readonly void Put(byte[]? src, int srcOffset, int dstOffset, int len) {
590590
if (len == 0 || src == null)
591591
return;
592592
Assumes.NotNull(m_data);
@@ -597,7 +597,7 @@ public void Put(byte[]? src, int srcOffset, int dstOffset, int len) {
597597
/// Copy the given single byte to this Msg's Data buffer.
598598
/// </summary>
599599
/// <param name="b">the source byte to copy from</param>
600-
public void Put(byte b)
600+
public readonly void Put(byte b)
601601
{
602602
Assumes.NotNull(m_data);
603603
m_data[m_offset] = b;
@@ -608,7 +608,7 @@ public void Put(byte b)
608608
/// </summary>
609609
/// <param name="b">the source byte to copy from</param>
610610
/// <param name="i">index within the internal Data array to copy that byte to</param>
611-
public void Put(byte b, int i)
611+
public readonly void Put(byte b, int i)
612612
{
613613
Assumes.NotNull(m_data);
614614
m_data[m_offset + i] = b;
@@ -620,7 +620,7 @@ public void Put(byte b, int i)
620620
/// <param name="encoding">The encoding to use for the writing</param>
621621
/// <param name="str">The string to write</param>
622622
/// <param name="index">The index to write the string to</param>
623-
public void Put(Encoding encoding, string str, int index)
623+
public readonly void Put(Encoding encoding, string str, int index)
624624
{
625625
Assumes.NotNull(m_data);
626626
encoding.GetBytes(str, 0, str.Length, m_data, m_offset + index);
@@ -640,7 +640,7 @@ public void Put(Span<byte> src, int offset)
640640
/// </summary>
641641
/// <param name="index">The index to access</param>
642642
/// <returns></returns>
643-
public byte this[int index]
643+
public readonly byte this[int index]
644644
{
645645
get
646646
{
@@ -727,7 +727,7 @@ public void Move(ref Msg src)
727727
/// Returns a new array containing the first <see cref="Size"/> bytes of <see cref="Data"/>.
728728
/// Deprecated: use <see cref="ToArray()"/>
729729
/// </summary>
730-
public byte[] CloneData()
730+
public readonly byte[] CloneData()
731731
{
732732
var data = new byte[Size];
733733

@@ -757,8 +757,8 @@ internal byte[] UnsafeToArray()
757757
return CloneData();
758758
}
759759

760-
internal byte[]? UnsafeData => m_data;
761-
internal int UnsafeOffset => m_offset;
760+
internal readonly byte[]? UnsafeData => m_data;
761+
internal readonly int UnsafeOffset => m_offset;
762762

763763
#endregion
764764
}

0 commit comments

Comments
 (0)