Skip to content

Commit 0061de0

Browse files
authored
Merge pull request #2647 from ksss/pread-pwrite
Add sig `IO#pread` and `IO#pwrite`
2 parents 4b9b5dd + 2bf1edc commit 0061de0

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

core/io.rbs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,61 @@ class IO < Object
14431443
#
14441444
def puts: (*untyped objects) -> nil
14451445

1446+
# <!--
1447+
# rdoc-file=io.c
1448+
# - pread(maxlen, offset) -> string
1449+
# - pread(maxlen, offset, out_string) -> string
1450+
# -->
1451+
# Behaves like IO#readpartial, except that it:
1452+
#
1453+
# * Reads at the given `offset` (in bytes).
1454+
# * Disregards, and does not modify, the stream's position (see
1455+
# [Position](rdoc-ref:IO@Position)).
1456+
# * Bypasses any user space buffering in the stream.
1457+
#
1458+
# Because this method does not disturb the stream's state (its position, in
1459+
# particular), `pread` allows multiple threads and processes to use the same IO
1460+
# object for reading at various offsets.
1461+
#
1462+
# f = File.open('t.txt')
1463+
# f.read # => "First line\nSecond line\n\nFourth line\nFifth line\n"
1464+
# f.pos # => 52
1465+
# # Read 12 bytes at offset 0.
1466+
# f.pread(12, 0) # => "First line\n"
1467+
# # Read 9 bytes at offset 8.
1468+
# f.pread(9, 8) # => "ne\nSecon"
1469+
# f.close
1470+
#
1471+
# Not available on some platforms.
1472+
#
1473+
def pread: (int maxlen, int offset, ?string out_string) -> String
1474+
1475+
# <!--
1476+
# rdoc-file=io.c
1477+
# - pwrite(object, offset) -> integer
1478+
# -->
1479+
# Behaves like IO#write, except that it:
1480+
#
1481+
# * Writes at the given `offset` (in bytes).
1482+
# * Disregards, and does not modify, the stream's position (see
1483+
# [Position](rdoc-ref:IO@Position)).
1484+
# * Bypasses any user space buffering in the stream.
1485+
#
1486+
# Because this method does not disturb the stream's state (its position, in
1487+
# particular), `pwrite` allows multiple threads and processes to use the same IO
1488+
# object for writing at various offsets.
1489+
#
1490+
# f = File.open('t.tmp', 'w+')
1491+
# # Write 6 bytes at offset 3.
1492+
# f.pwrite('ABCDEF', 3) # => 6
1493+
# f.rewind
1494+
# f.read # => "\u0000\u0000\u0000ABCDEF"
1495+
# f.close
1496+
#
1497+
# Not available on some platforms.
1498+
#
1499+
def pwrite: (_ToS object, int offset) -> Integer
1500+
14461501
# <!--
14471502
# rdoc-file=io.c
14481503
# - read(maxlen = nil, out_string = nil) -> new_string, out_string, or nil

test/stdlib/IO_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,40 @@ def test_readline
484484
io, :readline, "\n", 100, chomp: true
485485
end
486486
end
487+
488+
def test_pwrite
489+
Dir.mktmpdir do |dir|
490+
File.open(File.join(dir, "io-pwrite"), "w") do |io|
491+
with_int(0) do |offset|
492+
assert_send_type "(String, int) -> Integer",
493+
io, :pwrite, "hello", offset
494+
end
495+
end
496+
end
497+
rescue NotImplementedError
498+
omit "Not implemented"
499+
end
500+
501+
def test_pread
502+
IO.open(IO.sysopen(File.expand_path(__FILE__))) do |io|
503+
with_int(10) do |maxlen|
504+
with_int(0) do |offset|
505+
assert_send_type(
506+
"(int, int) -> String",
507+
io, :pread, maxlen, offset
508+
)
509+
with_string(+"buffer") do |buffer|
510+
assert_send_type(
511+
"(int, int, string) -> String",
512+
io, :pread, maxlen, offset, buffer
513+
)
514+
end
515+
end
516+
end
517+
end
518+
rescue NotImplementedError
519+
omit "Not implemented"
520+
end
487521
end
488522

489523
class IOWaitTest < Test::Unit::TestCase

0 commit comments

Comments
 (0)