Skip to content

Commit 5154c66

Browse files
authored
Merge pull request #5027 from ntrel/scopebuf-docs
Tweak std.internal.scopebuffer docs
2 parents de51b31 + c32a2f4 commit 5154c66

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

std/internal/scopebuffer.d

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ private import std.traits;
1515

1616
/**************************************
1717
* ScopeBuffer encapsulates using a local array as a temporary buffer.
18-
* It is initialized with the local array that should be large enough for
19-
* most uses. If the need exceeds the size, ScopeBuffer will resize it
20-
* using malloc() and friends.
18+
* It is initialized with a local array that should be large enough for
19+
* most uses. If the need exceeds that size, ScopeBuffer will reallocate
20+
* the data using its `realloc` function.
2121
*
22-
* ScopeBuffer cannot contain more than (uint.max-16)/2 elements.
22+
* ScopeBuffer cannot contain more than `(uint.max-16)/2` elements.
2323
*
24-
* ScopeBuffer is an OutputRange.
24+
* ScopeBuffer is an Output Range.
2525
*
26-
* Since ScopeBuffer potentially stores elements of type T in malloc'd memory,
26+
* Since ScopeBuffer may store elements of type `T` in `malloc`'d memory,
2727
* those elements are not scanned when the GC collects. This can cause
28-
* memory corruption. Do not use ScopeBuffer when elements of type T point
29-
* to the GC heap.
28+
* memory corruption. Do not use ScopeBuffer when elements of type `T` point
29+
* to the GC heap, except when a `realloc` function is provided which supports this.
3030
*
3131
* Example:
3232
---
@@ -80,15 +80,15 @@ string cat(string s1, string s2)
8080
* $(D scope(exit) textbuf.free();) for proper cleanup, and do not refer to a ScopeBuffer
8181
* instance's contents after $(D ScopeBuffer.free()) has been called.
8282
*
83-
* The realloc parameter defaults to C's realloc(). Another can be supplied to override it.
83+
* The `realloc` parameter defaults to C's `realloc()`. Another can be supplied to override it.
8484
*
8585
* ScopeBuffer instances may be copied, as in:
8686
---
8787
textbuf = doSomething(textbuf, args);
8888
---
8989
* which can be very efficent, but these must be regarded as a move rather than a copy.
9090
* Additionally, the code between passing and returning the instance must not throw
91-
* exceptions, otherwise when ScopeBuffer.free() is called, memory may get corrupted.
91+
* exceptions, otherwise when `ScopeBuffer.free()` is called, memory may get corrupted.
9292
*/
9393

9494
@system
@@ -111,9 +111,10 @@ struct ScopeBuffer(T, alias realloc = /*core.stdc.stdlib*/.realloc)
111111
* ubyte[10] tmpbuf = void;
112112
* auto sbuf = ScopeBuffer!ubyte(tmpbuf);
113113
* ---
114-
* If buf was created by the same realloc passed as a parameter
115-
* to ScopeBuffer, then the contents of ScopeBuffer can be extracted without needing
116-
* to copy them, and ScopeBuffer.free() will not need to be called.
114+
* Note:
115+
* If buf was created by the same `realloc` passed as a parameter
116+
* to `ScopeBuffer`, then the contents of `ScopeBuffer` can be extracted without needing
117+
* to copy them, and `ScopeBuffer.free()` will not need to be called.
117118
*/
118119
this(T[] buf)
119120
in
@@ -135,7 +136,7 @@ struct ScopeBuffer(T, alias realloc = /*core.stdc.stdlib*/.realloc)
135136

136137
/**************************
137138
* Releases any memory used.
138-
* This will invalidate any references returned by the [] operator.
139+
* This will invalidate any references returned by the `[]` operator.
139140
* A destructor is not used, because that would make it not POD
140141
* (Plain Old Data) and it could not be placed in registers.
141142
*/
@@ -149,14 +150,9 @@ struct ScopeBuffer(T, alias realloc = /*core.stdc.stdlib*/.realloc)
149150
used = 0;
150151
}
151152

152-
/****************************
153-
* Copying of ScopeBuffer is not allowed.
154-
*/
155-
//@disable this(this);
156-
157153
/************************
158154
* Append element c to the buffer.
159-
* This member function makes ScopeBuffer an OutputRange.
155+
* This member function makes `ScopeBuffer` an Output Range.
160156
*/
161157
void put(T c)
162158
{
@@ -178,7 +174,7 @@ struct ScopeBuffer(T, alias realloc = /*core.stdc.stdlib*/.realloc)
178174
* If $(D const(T)) can be converted to $(D T), then put will accept
179175
* $(D const(T)[]) as input. It will accept a $(D T[]) otherwise.
180176
*/
181-
private alias CT = Select!(is(const(T) : T), const(T), T);
177+
package alias CT = Select!(is(const(T) : T), const(T), T);
182178
/// ditto
183179
void put(CT[] s)
184180
{
@@ -195,10 +191,10 @@ struct ScopeBuffer(T, alias realloc = /*core.stdc.stdlib*/.realloc)
195191
}
196192

197193
/******
198-
* Retrieve a slice into the result.
199194
* Returns:
200-
* A slice into the temporary buffer that is only
201-
* valid until the next put() or ScopeBuffer goes out of scope.
195+
* A slice into the temporary buffer.
196+
* Warning:
197+
* The result is only valid until the next `put()` or `ScopeBuffer` goes out of scope.
202198
*/
203199
@system inout(T)[] opSlice(size_t lower, size_t upper) inout
204200
in
@@ -221,7 +217,7 @@ struct ScopeBuffer(T, alias realloc = /*core.stdc.stdlib*/.realloc)
221217

222218
/*******
223219
* Returns:
224-
* the element at index i.
220+
* The element at index i.
225221
*/
226222
ref inout(T) opIndex(size_t i) inout
227223
{
@@ -231,7 +227,7 @@ struct ScopeBuffer(T, alias realloc = /*core.stdc.stdlib*/.realloc)
231227

232228
/***
233229
* Returns:
234-
* the number of elements in the ScopeBuffer
230+
* The number of elements in the `ScopeBuffer`.
235231
*/
236232
@property size_t length() const
237233
{
@@ -240,7 +236,7 @@ struct ScopeBuffer(T, alias realloc = /*core.stdc.stdlib*/.realloc)
240236

241237
/***
242238
* Used to shrink the length of the buffer,
243-
* typically to 0 so the buffer can be reused.
239+
* typically to `0` so the buffer can be reused.
244240
* Cannot be used to extend the length of the buffer.
245241
*/
246242
@property void length(size_t i)
@@ -367,25 +363,20 @@ unittest
367363
}
368364

369365
/*********************************
370-
* This is a slightly simpler way to create a ScopeBuffer instance
371-
* that uses type deduction.
366+
* Creates a `ScopeBuffer` instance using type deduction - see
367+
* $(LREF .ScopeBuffer.this) for details.
372368
* Params:
373369
* tmpbuf = the initial buffer to use
374370
* Returns:
375-
* an instance of ScopeBuffer
376-
* Example:
377-
---
378-
ubyte[10] tmpbuf = void;
379-
auto sb = scopeBuffer(tmpbuf);
380-
scope(exit) sp.free();
381-
---
371+
* An instance of `ScopeBuffer`.
382372
*/
383373

384374
auto scopeBuffer(T)(T[] tmpbuf)
385375
{
386376
return ScopeBuffer!T(tmpbuf);
387377
}
388378

379+
///
389380
unittest
390381
{
391382
ubyte[10] tmpbuf = void;

0 commit comments

Comments
 (0)