Skip to content

Commit

Permalink
Upgrade File.byLineCopy to be @safe to use
Browse files Browse the repository at this point in the history
  • Loading branch information
0xEAB committed Jan 2, 2025
1 parent ad8ee55 commit bed9016
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -2409,7 +2409,7 @@ void main()
private struct ByLineCopy(Char, Terminator)
{
private:
import std.typecons : SafeRefCounted, RefCountedAutoInitialize;
import std.typecons : borrow, RefCountedAutoInitialize, SafeRefCounted;

/* Ref-counting stops the source range's ByLineCopyImpl
* from getting out of sync after the range is copied, e.g.
Expand All @@ -2425,19 +2425,24 @@ void main()
impl = Impl(f, kt, terminator);
}

@property bool empty()
/* Verifiably `@safe` when built with -preview=DIP1000. */
@property bool empty() @trusted
{
return impl.refCountedPayload.empty;
// Using `ref` is actually necessary here.
return impl.borrow!((ref i) => i.empty);
}

@property Char[] front()
/* Verifiably `@safe` when built with -preview=DIP1000. */
@property Char[] front() @trusted
{
return impl.refCountedPayload.front;
// Using `ref` is likely optional here.
return impl.borrow!((ref i) => i.front);
}

void popFront()
/* Verifiably `@safe` when built with -preview=DIP1000. */
void popFront() @trusted
{
impl.refCountedPayload.popFront();
impl.borrow!((ref i) => i.popFront());
}
}

Expand Down Expand Up @@ -2666,7 +2671,7 @@ $(REF readText, std,file)
assert(!file.isOpen);
}

@system unittest
@safe unittest
{
static import std.file;
auto deleteme = testFilename();
Expand Down Expand Up @@ -4749,16 +4754,16 @@ struct lines
auto myLines = lines(f);
foreach (string line; myLines)
continue;
}


auto myByLineCopy = f.byLineCopy; // but cannot safely iterate yet
/*
still `@system`:
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.empty`
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.popFront`
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.front`
*/
//foreach (line; myByLineCopy)
// continue;
{
auto f = File(deleteMe, "r");
scope(exit) { f.close(); }

auto myByLineCopy = f.byLineCopy;
foreach (line; myByLineCopy)
continue;
}
}

Expand Down

0 comments on commit bed9016

Please sign in to comment.