[phobos] UnbufferedFile, or, abstracting the File ranges

Andrei Alexandrescu andrei at erdani.com
Wed May 12 07:12:44 PDT 2010


Good points. A possible way to improve rawRead is to fork it into a safe 
and an unsafe version, depending on T.

Andrei

Lars Tandle Kyllingstad wrote:
> On Tue, 2010-05-11 at 11:18 -0700, Andrei Alexandrescu wrote:
>> Lars Tandle Kyllingstad wrote:
>>> Yeah, the ubyte functions were just examples.  My intention was to use
>>> templates:
>>>
>>>   size_t read(T)(ref T[] b);
>>>   size_t write(T)(T[] b);
>>>
>>> Then you get a sensible error message if the number of raw bytes read
>>> isn't a multiple of the size of your target type.  Also, the returned
>>> number is the length of the resulting array, and not the number of raw
>>> bytes read.
>>>
>>> Thanks for the tips! :)
>> That would make the functions type-unsafe. You'd need to limit T to 
>> types that have no pointers (at least). Whoa, serialization rears its 
>> ugly head.
> 
> 
> But you'd have the same problem with void[], wouldn't you?  I mean, this
> compiles and runs just fine:
> 
>         void read(void[] buf) { }
>         
>         struct S { int* p; }
>         
>         auto a = new S[3];
>         read(a);
> 
> At least with templates one can restrict T to built-in types, and if
> anyone wants to read/write compound types they can just set T to void.
> This has the added advantage of making it very explicit that if you do
> that, you're on your own.
> 
> Another potential gotcha with
> 
>         size_t read(void[]);
>         
> is that even if you pass it an array of ints, say, the return value will
> still be the number of raw bytes read (which, to make matters worse, may
> or may not be a multiple of int.sizeof).
> 
> I would also like to point out that all the read/write functions of
> std.stdio.File, including rawRead() and rawWrite(), are templated.  Has
> there been any problems with these?
> 
> -Lars
> 
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos


More information about the phobos mailing list