Skip to content

Commit 5b0fe77

Browse files
committed
Upgrade File.byLineCopy to be @safe to use
1 parent 0e97dc5 commit 5b0fe77

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

std/stdio.d

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,7 +2409,7 @@ void main()
24092409
private struct ByLineCopy(Char, Terminator)
24102410
{
24112411
private:
2412-
import std.typecons : SafeRefCounted, RefCountedAutoInitialize;
2412+
import std.typecons : borrow, RefCountedAutoInitialize, SafeRefCounted;
24132413

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

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

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

2438-
void popFront()
2442+
/* Verifiably `@safe` when built with -preview=DIP1000. */
2443+
void popFront() @trusted
24392444
{
2440-
impl.refCountedPayload.popFront();
2445+
impl.borrow!((ref i) => i.popFront());
24412446
}
24422447
}
24432448

@@ -2666,7 +2671,7 @@ $(REF readText, std,file)
26662671
assert(!file.isOpen);
26672672
}
26682673

2669-
@system unittest
2674+
@safe unittest
26702675
{
26712676
static import std.file;
26722677
auto deleteme = testFilename();
@@ -4749,16 +4754,16 @@ struct lines
47494754
auto myLines = lines(f);
47504755
foreach (string line; myLines)
47514756
continue;
4757+
}
4758+
47524759

4753-
auto myByLineCopy = f.byLineCopy; // but cannot safely iterate yet
4754-
/*
4755-
still `@system`:
4756-
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.empty`
4757-
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.popFront`
4758-
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.front`
4759-
*/
4760-
//foreach (line; myByLineCopy)
4761-
// continue;
4760+
{
4761+
auto f = File(deleteMe, "r");
4762+
scope(exit) { f.close(); }
4763+
4764+
auto myByLineCopy = f.byLineCopy;
4765+
foreach (line; myByLineCopy)
4766+
continue;
47624767
}
47634768
}
47644769

0 commit comments

Comments
 (0)