std.Stream.InputStream convenience functions
BBasile via Digitalmars-d
digitalmars-d at puremagic.com
Sat Sep 5 16:00:41 PDT 2015
On Saturday, 5 September 2015 at 19:59:03 UTC, bitwise wrote:
> On Saturday, 5 September 2015 at 06:15:17 UTC, Jonathan M Davis
> wrote:
>> On Friday, 4 September 2015 at 22:36:01 UTC, bitwise wrote:
>>> On Friday, 4 September 2015 at 14:40:43 UTC, Jonathan M Davis
>>> wrote:
>>>> [...]
>>>
>>> Wow...this is surprising. At first glance, the streams seemed
>>> to be fairly well done. What's the reason for them being
>>> removed? and what is meant to be the replacement?
>>
>> It's old (from D1) and does not follow the current best
>> practices or idioms. If we're going to have a stream solution,
>> it needs to be range-based. That being said, ranges in and of
>> themselves get pretty close to streams, and there simply
>> hasn't been a push to come up with a range-based stream
>> solution where the pieces that aren't already part of ranges
>> are taken care of. std.stream has been marked as scheduled for
>> deprecation for years, and no one has bothered to replace it.
>> It comes up periodically, but no one seems to care enough to
>> do anything about it. So, rather than leave it in that state,
>> we decided that it would be better off to just deprecate it
>> and then get rid of it rather than leave it in limbo with the
>> documentation saying that it's not up to our standards and
>> that we're going to get rid of it at some point. At some
>> point, someone may come up with a range-based stream solution
>> and get it into Phobos, but until then, we'll just use
>> straight up ranges, which come close enough for most cases.
>> std.stdio and std.mmfile make it quite possible to operate on
>> files with ranges in a manner similar to streams, and
>> std.stdio allows you to operate on stdin and stdout in a
>> similar manner (since they're std.stdio.Files).
>>
>> That being said, std.stream is boost-licensed, and anyone is
>> free to copy it or fork it so long as the copyright notice is
>> left on it. So, anyone who wants to use std.stream in their
>> own code even after it's no longer in Phobos is free to do so.
>> It's just that the std.stream code is then going to have to be
>> part of their project rather than Phobos.
>>
>> - Jonathan M Davis
>
> Thanks for the explanation, but could you give an example of
> how Stream would be rangified?
this was not addressed to me but here is how it should be done:
---
module runnable;
import std.stdio;
import std.stream;
import std.algorithm;
class RangifiedStream: MemoryStream
{
auto range()
{
return Range(this);
}
private struct Range
{
MemoryStream _str;
ulong _curr;
this(MemoryStream str)
{
_str = str;
}
void popFront()
{
_curr += 1;
}
@property ubyte front()
{
ubyte result;
_str.position = _curr;
_str.read(result);
_str.position = _str.position - 1;
return result;
}
@property bool empty()
{
return _curr == _str.size;
}
}
}
void main(string[] args)
{
import std.range;
assert( isInputRange!(RangifiedStream.Range));
RangifiedStream str = new RangifiedStream;
ubyte[] src = [0,1,2,3,4,5,6,7,8,9];
str.write(src);
str.range.each!(a => a.writeln);
}
---
The range has a reference to a stream. The range uses its own
position and this is important since several ranges may co-exist
at the same time. Here you just have a byte InputRange but it
works...
For FileStream the performances will be terrible (bad), because
the position is hold by a a structure specific to the OS...
I think I'll add this to my iz streams classes.
More information about the Digitalmars-d
mailing list