@@ -32,4 +32,100 @@ uint Size {
3232 /// <param name="length">The length of the slice</param>
3333 /// <returns>A byte list</returns>
3434 public IList < byte > GetSlice ( int address , int length ) ;
35+
36+ /// <summary>
37+ /// Attempts to get a span from the reader/writer.
38+ /// </summary>
39+ /// <param name="startAddress">The starting address for the returned span.</param>
40+ /// <param name="span">A span containing all the bytes in the requested memory.</param>
41+ /// <param name="access">Specifies the type of memory access that is requested for the span.</param>
42+ /// <returns><see langword="true"/> if the span was successfully retrieved; otherwise, <see langword="false"/>.</returns>
43+ public virtual bool TryGetSpan ( out uint startAddress , out Span < byte > span , MemoryAccess access ) {
44+ startAddress = 0 ;
45+ if ( ( int ) Size >= 0 ) {
46+ return TryGetSpan ( 0 , ( int ) Size , out span , access ) ;
47+ }
48+
49+ span = [ ] ;
50+ return false ;
51+ }
52+
53+ /// <summary>
54+ /// Attempts to get a read only span from the reader/writer.
55+ /// </summary>
56+ /// <param name="startAddress">The starting address for the returned span.</param>
57+ /// <param name="span">A read only span containing all the bytes in the requested memory.</param>
58+ /// <param name="access">Specifies the type of memory access that is requested for the span.</param>
59+ /// <returns><see langword="true"/> if the span was successfully retrieved; otherwise, <see langword="false"/>.</returns>
60+ public virtual bool TryGetSpan ( out uint startAddress , out ReadOnlySpan < byte > span , MemoryAccess access ) {
61+ bool result = TryGetSpan ( out startAddress , out Span < byte > mutableSpan , access ) ;
62+ span = mutableSpan ;
63+ return result ;
64+ }
65+
66+ /// <summary>
67+ /// Attempts to get a span from a portion of the reader/writer.
68+ /// </summary>
69+ /// <param name="startAddress">The starting address to request bytes from.</param>
70+ /// <param name="span">A span containing the remaining bytes starting at the given address.</param>
71+ /// <param name="access">Specifies the type of memory access that is requested for the span.</param>
72+ /// <returns><see langword="true"/> if the span was successfully retrieved; otherwise, <see langword="false"/>.</returns>
73+ public virtual bool TryGetSpan ( uint startAddress , out Span < byte > span , MemoryAccess access ) {
74+ if ( ( long ) Size - startAddress is >= 0 and <= int . MaxValue ) {
75+ return TryGetSpan ( startAddress , ( int ) ( Size - startAddress ) , out span , access ) ;
76+ }
77+
78+ span = [ ] ;
79+ return false ;
80+ }
81+
82+ /// <summary>
83+ /// Attempts to get a read only span from a portion of the reader/writer.
84+ /// </summary>
85+ /// <param name="startAddress">The starting address to request bytes from.</param>
86+ /// <param name="span">A span containing the remaining bytes starting at the given address.</param>
87+ /// <param name="access">Specifies the type of memory access that is requested for the span.</param>
88+ /// <returns><see langword="true"/> if the span was successfully retrieved; otherwise, <see langword="false"/>.</returns>
89+ public virtual bool TryGetSpan ( uint startAddress , out ReadOnlySpan < byte > span , MemoryAccess access ) {
90+ bool result = TryGetSpan ( startAddress , out Span < byte > mutableSpan , access ) ;
91+ span = mutableSpan ;
92+ return result ;
93+ }
94+
95+ /// <summary>
96+ /// Attempts to get a span from a portion of the reader/writer.
97+ /// </summary>
98+ /// <param name="startAddress">The starting address to request bytes from.</param>
99+ /// <param name="length">The number of bytes requested. A negative length should always result in a failure.</param>
100+ /// <param name="span">A span containing the number of requested bytes starting at the given address.</param>
101+ /// <param name="access">Specifies the type of memory access that is requested for the span.</param>
102+ /// <returns><see langword="true"/> if the span was successfully retrieved; otherwise, <see langword="false"/>.</returns>
103+ /// <remarks>
104+ /// Implementors should always return a span with <paramref name="length"/> bytes if successful. Callers should
105+ /// only assume that all implementors will return a span with <em>at least</em> <paramref name="length"/> bytes
106+ /// on success.
107+ /// </remarks>
108+ public virtual bool TryGetSpan ( uint startAddress , int length , out Span < byte > span , MemoryAccess access ) {
109+ span = [ ] ;
110+ return false ;
111+ }
112+
113+ /// <summary>
114+ /// Attempts to get a read only span from a portion of the reader/writer.
115+ /// </summary>
116+ /// <param name="startAddress">The starting address to request bytes from.</param>
117+ /// <param name="length">The number of bytes requested. A negative length should always result in a failure.</param>
118+ /// <param name="span">A span containing the number of requested bytes starting at the given address.</param>
119+ /// <param name="access">Specifies the type of memory access that is requested for the span.</param>
120+ /// <returns><see langword="true"/> if the span was successfully retrieved; otherwise, <see langword="false"/>.</returns>
121+ /// <remarks>
122+ /// Implementors should always return a span with <paramref name="length"/> bytes if successful. Callers should
123+ /// only assume that all implementors will return a span with <em>at least</em> <paramref name="length"/> bytes
124+ /// on success.
125+ /// </remarks>
126+ public virtual bool TryGetSpan ( uint startAddress , int length , out ReadOnlySpan < byte > span , MemoryAccess access ) {
127+ bool result = TryGetSpan ( startAddress , length , out Span < byte > mutableSpan , access ) ;
128+ span = mutableSpan ;
129+ return result ;
130+ }
35131}
0 commit comments