Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make File.byLineCopy usable in @safe contexts #10606

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading