www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - safe std.file.read

reply WebFreak001 <d.forum webfreak.org> writes:
I was wondering, how are you supposed to use std.file : read in 
 safe code when it returns a void[] but you want to get all bytes 
in the file?

Is void[] really the correct type it should be returning instead 
of ubyte[] when it just reads a (binary) file to memory? Or 
should void[] actually be castable to ubyte[] in  safe code?
Jan 06 2020
next sibling parent Dennis <dkorpel gmail.com> writes:
I would say it should return a ubyte[].

On Monday, 6 January 2020 at 10:07:37 UTC, WebFreak001 wrote:
 Or should void[] actually be castable to ubyte[] in  safe code?
Definitely not with the current semantics, since a void[] can alias pointers in safe code. See: https://issues.dlang.org/show_bug.cgi?id=20345
Jan 06 2020
prev sibling next sibling parent Dominikus Dittes Scherkl <dominikus.scherkl continental-corporation.com> writes:
On Monday, 6 January 2020 at 10:07:37 UTC, WebFreak001 wrote:
 I was wondering, how are you supposed to use std.file : read in 
  safe code when it returns a void[] but you want to get all 
 bytes in the file?

 Is void[] really the correct type it should be returning 
 instead of ubyte[] when it just reads a (binary) file to 
 memory? Or should void[] actually be castable to ubyte[] in 
  safe code?
I definitely think it should return ubyte[]. void[] is a very special abstraction that shouldn't be used at all if you don't know very well what you're doing.
Jan 06 2020
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/6/20 5:07 AM, WebFreak001 wrote:
 I was wondering, how are you supposed to use std.file : read in  safe 
 code when it returns a void[] but you want to get all bytes in the file?
 
 Is void[] really the correct type it should be returning instead of 
 ubyte[] when it just reads a (binary) file to memory? Or should void[] 
 actually be castable to ubyte[] in  safe code?
I feel like this conversation has been had before. But I think it should be ubyte[]. Not sure why it's void[]. Perhaps for symmetry with write, which takes void[] (for good reason)? -Steve
Jan 06 2020
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, January 6, 2020 8:52:01 AM MST Steven Schveighoffer via 
Digitalmars-d-learn wrote:
 On 1/6/20 5:07 AM, WebFreak001 wrote:
 I was wondering, how are you supposed to use std.file : read in  safe
 code when it returns a void[] but you want to get all bytes in the file?

 Is void[] really the correct type it should be returning instead of
 ubyte[] when it just reads a (binary) file to memory? Or should void[]
 actually be castable to ubyte[] in  safe code?
I feel like this conversation has been had before. But I think it should be ubyte[]. Not sure why it's void[]. Perhaps for symmetry with write, which takes void[] (for good reason)?
I think that in previous discussions, it was decided that in general, when you're dealing with something like reading from / write to a file or a socket, writing should accept void[], because then you can write any binary data to it without casting (including objects which are being serialized), whereas reading should give you ubyte[] or const(ubyte)[], because what you're getting from the OS is bytes of data, and it's up to the program to figure out what to do with them. - Jonathan M Davis
Jan 06 2020
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/6/20 5:07 AM, WebFreak001 wrote:
 Or should void[] 
 actually be castable to ubyte[] in  safe code?
No, because you can implicitly cast anything to void[], including pointer arrays. Possibly const(ubyte[]). -Steve
Jan 06 2020