Make sure lifetime of helper structs is less than owning struct

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 15 16:08:24 UTC 2021


On 11/15/21 10:56 AM, WebFreak001 wrote:
> I have an API with some struct like a file reader. I want to add 
> byChunks-like functionality to it, so I'm trying to implement it with a 
> helper struct that implements opApply. I have disabled copying the file 
> reader struct because it cleans up the resources once it goes out of 
> scope, however now I need to temporarily save the resources in the 
> helper struct to be able to read from it.
> 
> How can I make sure that the foreach helper struct (and with that the 
> copies of the resources) cannot be used once the owning struct goes out 
> of scope?
> 
> ```d
> ByChunk helper;
> {
>      auto file = FileReader(x);
>      helper = file.byChunk;
> }
> helper.popFront; // crash - I want the compiler to disallow this
> ```
> 
> is this currently possible or maybe possible with DIP1000?

Or maybe just use reference counting to avoid the problem altogether. 
That's what iopipe/std.io does.

-Steve


More information about the Digitalmars-d-learn mailing list