save and load a 2d array to a file

Steven Schveighoffer schveiguy at gmail.com
Tue Apr 19 15:04:34 UTC 2022


On 4/19/22 5:12 AM, Stanislav Blinov wrote:
> On Tuesday, 19 April 2022 at 06:05:27 UTC, Ali Çehreli wrote:
> 
>> One quirk of rawWrite and rawRead is that they want slices of objects. 
>> It is a little awkward when there is just one thing to write and read. 
>> Uncompiled but something like this:
>>
>>   int i = 42;
>>   file.rawWrite(*cast((int[1]*)(&i)));  // Casted to be an array of 1
> 
> Yuck :)
> 
> ```d
> file.rawWrite((&i)[0..1]);
> ```

Hm... an "asStaticArray" or something similar would be useful for such 
things (and safe too):

```d
ref T[1] asStaticArray(T)(return ref T item) @trusted
{
    return *(cast(T[1])&item);
}
```

-Steve


More information about the Digitalmars-d-learn mailing list