-
Notifications
You must be signed in to change notification settings - Fork 10
[IO] add handle copy function #120
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?hh | ||
/* | ||
* Copyright (c) 2004-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace HH\Lib\IO; | ||
|
||
use namespace HH\Lib\Str; | ||
|
||
/** | ||
* Copies content from the given $source read handle to the $target write handle | ||
* until the eol of $source is reached. | ||
* | ||
* If `$chunk_size` is `null`, there is no limit on the chunk size - this function will | ||
* copy the content of $source all at once. | ||
*/ | ||
async function copy( | ||
<<__AcceptDisposable>> ReadHandle $source, | ||
<<__AcceptDisposable>> WriteHandle $target, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personal preference, but isn't |
||
?int $chunk_size = null, | ||
azjezz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
?float $timeout_seconds = null, | ||
): Awaitable<void> { | ||
if (!$source->isEndOfFile()) { | ||
$content = await $source->readAsync($chunk_size, $timeout_seconds); | ||
await $target->writeAsync($content, $timeout_seconds); | ||
} | ||
|
||
if (!$source->isEndOfFile()) { | ||
azjezz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await copy($source, $target, $chunk_size, $timeout_seconds); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you implemented this via recursive call because of the DontAwaitInALoopLinter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yes
why violate the linter rule if we can avoid it? 😛 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suppress the linter.
Because violating the linter rule doesn't consume stack frames, leading to fatals on large files ;) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2004-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
use namespace HH\Lib\IO; | ||
use namespace HH\Lib\File; | ||
|
||
use function Facebook\FBExpect\expect; // @oss-enable | ||
use type Facebook\HackTest\HackTest; // @oss-enable | ||
// @oss-disable: use type HackTest; | ||
|
||
// @oss-disable: <<Oncalls('hack')>> | ||
final class CopyTest extends HackTest { | ||
public async function testCopy(): Awaitable<void> { | ||
await using $source = File\temporary_file(); | ||
await using $target = File\temporary_file(); | ||
|
||
await $source->writeAsync('Hello, World!'); | ||
await $source->seekAsync(0); | ||
|
||
expect($source->isEndOfFile())->toBeFalse(); | ||
expect($target->isEndOfFile())->toBeFalse(); | ||
azjezz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expect(await $target->readAsync())->toBeSame(''); | ||
|
||
await IO\copy($source, $target); | ||
|
||
expect($source->isEndOfFile())->toBeTrue(); | ||
expect($target->isEndOfFile())->toBeTrue(); | ||
|
||
// Assert that the content has been copied. | ||
await $target->seekAsync(0); | ||
expect(await $target->readAsync())->toBeSame('Hello, World!'); | ||
|
||
// Assert that the original file contains the same content. not modifications are made. | ||
await $source->seekAsync(0); | ||
expect(await $source->readAsync())->toBeSame('Hello, World!'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To match the naming conventions,
copyAsync()
or is this handled by thegen
rewriter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure about this. other functions in
File\
don't use the_async
suffix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be copy_async
Other functions in File\ do not return Awaitables, so do not have the suffix (if that's not the case, it's a bug that needs fixing)