Skip to content

Commit db002ce

Browse files
committed
Compatibility with iterable readable streams
1 parent f4bd0df commit db002ce

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

src/Driver/BlockingFile.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
namespace Amp\File\Driver;
44

55
use Amp\ByteStream\ClosedException;
6+
use Amp\ByteStream\ReadableStreamIteratorAggregate;
67
use Amp\ByteStream\StreamException;
78
use Amp\Cancellation;
89
use Amp\DeferredFuture;
910
use Amp\File\File;
1011

11-
final class BlockingFile implements File
12+
/**
13+
* @implements \IteratorAggregate<int, string>
14+
*/
15+
final class BlockingFile implements File, \IteratorAggregate
1216
{
17+
use ReadableStreamIteratorAggregate;
18+
1319
/** @var resource|null */
1420
private $handle;
1521
private string $path;

src/Driver/ParallelFile.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Amp\File\Driver;
44

55
use Amp\ByteStream\ClosedException;
6+
use Amp\ByteStream\ReadableStreamIteratorAggregate;
67
use Amp\ByteStream\StreamException;
78
use Amp\Cancellation;
89
use Amp\DeferredFuture;
@@ -15,8 +16,13 @@
1516
use Revolt\EventLoop;
1617
use function Amp\async;
1718

18-
final class ParallelFile implements File
19+
/**
20+
* @implements \IteratorAggregate<int, string>
21+
*/
22+
final class ParallelFile implements File, \IteratorAggregate
1923
{
24+
use ReadableStreamIteratorAggregate;
25+
2026
private readonly Internal\FileWorker $worker;
2127

2228
private ?int $id;

src/Driver/StatusCachingFile.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
namespace Amp\File\Driver;
44

5+
use Amp\ByteStream\ReadableStreamIteratorAggregate;
56
use Amp\Cancellation;
67
use Amp\File\File;
78

8-
final class StatusCachingFile implements File
9+
/**
10+
* @implements \IteratorAggregate<int, string>
11+
*/
12+
final class StatusCachingFile implements File, \IteratorAggregate
913
{
14+
use ReadableStreamIteratorAggregate;
15+
1016
private readonly File $file;
1117

1218
private readonly \Closure $invalidateCallback;

src/Internal/QueuedWritesFile.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
namespace Amp\File\Internal;
44

55
use Amp\ByteStream\ClosedException;
6+
use Amp\ByteStream\ReadableStreamIteratorAggregate;
7+
use Amp\Cancellation;
68
use Amp\File\File;
79
use Amp\File\PendingOperationError;
810
use Amp\Future;
911
use function Amp\async;
1012

11-
/** @internal */
12-
abstract class QueuedWritesFile implements File
13+
/**
14+
* @internal
15+
* @implements \IteratorAggregate<int, string>
16+
*/
17+
abstract class QueuedWritesFile implements File, \IteratorAggregate
1318
{
19+
use ReadableStreamIteratorAggregate;
20+
1421
/** @var \SplQueue<Future<null>> */
1522
protected readonly \SplQueue $queue;
1623

@@ -39,6 +46,11 @@ public function __destruct()
3946
async($this->close(...));
4047
}
4148

49+
abstract public function read(
50+
?Cancellation $cancellation = null,
51+
int $length = self::DEFAULT_READ_LENGTH,
52+
): ?string;
53+
4254
/**
4355
* @return Future<null>
4456
*/
@@ -114,21 +126,12 @@ public function seek(int $position, int $whence = \SEEK_SET): int
114126
throw new PendingOperationError;
115127
}
116128

117-
switch ($whence) {
118-
case self::SEEK_SET:
119-
$this->position = $position;
120-
break;
121-
case self::SEEK_CUR:
122-
$this->position += $position;
123-
break;
124-
case self::SEEK_END:
125-
$this->position = $this->size + $position;
126-
break;
127-
default:
128-
throw new \Error("Invalid whence parameter; SEEK_SET, SEEK_CUR or SEEK_END expected");
129-
}
130-
131-
return $this->position;
129+
return match ($whence) {
130+
self::SEEK_SET => $this->position = $position,
131+
self::SEEK_CUR => $this->position += $position,
132+
self::SEEK_END => $this->position = $this->size + $position,
133+
default => throw new \Error("Invalid whence parameter; SEEK_SET, SEEK_CUR or SEEK_END expected"),
134+
};
132135
}
133136

134137
public function tell(): int

0 commit comments

Comments
 (0)