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 1, 2025
1 parent ff7b0d1 commit bc8a520
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 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,19 @@ void main()
impl = Impl(f, kt, terminator);
}

@property bool empty()
@property bool empty() @safe
{
return impl.refCountedPayload.empty;
return impl.borrow!(i => i.empty);
}

@property Char[] front()
@property Char[] front() @safe
{
return impl.refCountedPayload.front;
return impl.borrow!(i => i.front);
}

void popFront()
void popFront() @safe
{
impl.refCountedPayload.popFront();
impl.borrow!(i => i.popFront());
}
}

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

@system unittest
@safe unittest
{
static import std.file;
auto deleteme = testFilename();
Expand Down Expand Up @@ -4750,15 +4750,9 @@ struct lines
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 myByLineCopy = f.byLineCopy;
foreach (line; myByLineCopy)
continue;
}
}

Expand Down

0 comments on commit bc8a520

Please sign in to comment.