schveiguy / io

Core I/O functionality
Boost Software License 1.0
25 stars 9 forks source link

Use with a growing buffer #50

Open aminya opened 3 years ago

aminya commented 3 years ago

How can I use this package with a growing buffer?

I tried a dynamic array, but it doesn't work

    ubyte[] buf;
    auto file = File(filePath, Mode.readWrite);
    file.read(buf);

    import std: writeln;
    writeln(buf); // buf is empty
schveiguy commented 3 years ago

This package does not do buffering, it is strictly an abstraction of an i/o subsystem, and the basis of other packages which might do buffering or whatever else. If you want buffering, I'd suggest using iopipe which is built to work on top of this package, and provides a buffering system.

Your code basically says "read 0 bytes from the input", and then writes that empty buffer to stdout.

aminya commented 3 years ago

I couldn't find a simple example that allows me to read text from a file. I want to see if this library gives better performance over the following code that I have.

   import std.file : readText, write;

   const string str = readText(file);
   const strModified = doThingsWithString(str);
   write(file, strModified);
schveiguy commented 3 years ago

read and readText use the file size to preallocate the result. I doubt you get better performance by using a growing buffer. I also wouldn’t expect much performance difference with std.io if you did the same optimization.