xbuf ~master
Extensible self-loading buffer
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
xbuf
A buffer that loads data from external source automatically managing its size.
The key parameters are:
capacity
- initial capacityminLoading
- minimum number of bytes to load in one call to the data sourceloader
- a delegate that should load data into buffer, returning number of loaded byts, 0 on end of input and negative to indicate errorgrowBy
- granularity of allocation, uses OS page size by default
The application can manipulate contents of the buffer by calling load
, resize
and compact
. Load loads more data into the buffer, extending the buffer to load at least minLoading
bytes. load
is potentially blocking if loader is doing synchronious I/O.
resize
allows application to manually resize the buffer for instance to shrink the buffer or to more agressively extend the buffer - for instance if the protocol contains payload size. compact
discards a number of bytes up to size
, it should be called by application after it processed a number of bytes and no longer needs them. compact
invalidates all slices into buffer be careful not to compact
while holding references to the buffer or patch them accordingly.
///
unittest {
// capacity 32, min loading size 16, grow granularity 10
XBuf buf = XBuf(32, 16, (slice) {
foreach (i, ref s; slice) {
s = cast(ubyte)i;
}
return cast(int)slice.length;
}, 10);
buf.load();
// loaded full buffer of 0..32
foreach (i; 0..32) {
assert(buf[i] == i);
}
buf.load();
// capacity is 32+16 rounded to 10
assert(buf.size == 50);
foreach (i; 0..32){
assert(buf[i] == i);
}
foreach (i; 32..50) {
assert(buf[i] == i - 32);
}
buf.compact(20);
// all of the first 20 bytes were overwritten
foreach (i; 20..32) {
assert(buf[i-20] == i);
}
}
- Registered by Dmitry Olshansky
- ~master released a month ago
- DmitryOlshansky/xbuf
- BSL-1.0
- Copyright © 2024, Dmitry Olshansky
- Authors:
- Dependencies:
- none
- Versions:
-
0.1.1 2024-Oct-14 0.1.0 2024-Jul-05 ~master 2024-Oct-14 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
1 downloads this month
-
2 downloads total
-
- Score:
- 0.3
- Short URL:
- xbuf.dub.pm