Skip to content

Commit

Permalink
Change IO#write, IO#skip, IO.copy to return Int64 (crystal-lang#9363)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian J. Cardiff authored May 28, 2020
1 parent e43364f commit 6380fa9
Show file tree
Hide file tree
Showing 26 changed files with 119 additions and 119 deletions.
4 changes: 2 additions & 2 deletions spec/std/http/request_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ private class EmptyIO < IO
0
end

def write(slice : Bytes) : UInt64
slice.size.to_u64
def write(slice : Bytes) : Int64
slice.size.to_i64
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/http/server/response_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ private class ReverseResponseOutput < IO
def initialize(@output : IO)
end

def write(slice : Bytes) : UInt64
def write(slice : Bytes) : Int64
slice.reverse_each do |byte|
@output.write_byte(byte)
end
slice.size.to_u64
slice.size.to_i64
end

def read(slice : Bytes)
Expand Down
8 changes: 4 additions & 4 deletions spec/std/io/io_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private class SimpleIOMemory < IO
count
end

def write(slice : Bytes) : UInt64
def write(slice : Bytes) : Int64
count = slice.size
new_bytesize = bytesize + count
if new_bytesize > @capacity
Expand All @@ -56,7 +56,7 @@ private class SimpleIOMemory < IO
slice.copy_to(@buffer + @bytesize, count)
@bytesize += count

slice.size.to_u64
slice.size.to_i64
end

def to_slice
Expand Down Expand Up @@ -99,8 +99,8 @@ private class OneByOneIO < IO
1
end

def write(slice : Bytes) : UInt64
slice.size.to_u64
def write(slice : Bytes) : Int64
slice.size.to_i64
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/io/sized_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ private class NoPeekIO < IO
0
end

def write(bytes : Bytes) : UInt64
0u64
def write(bytes : Bytes) : Int64
0i64
end

def peek
Expand Down
4 changes: 2 additions & 2 deletions spec/support/io.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class RaiseIOError < IO
raise IO::Error.new("...")
end

def write(slice : Bytes) : UInt64
def write(slice : Bytes) : Int64
@writes += 1
raise IO::Error.new("...") if @raise_on_write
slice.size.to_u64
slice.size.to_i64
end

def flush
Expand Down
6 changes: 3 additions & 3 deletions src/compress/deflate/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ class Compress::Deflate::Writer < IO
end

# See `IO#write`.
def write(slice : Bytes) : UInt64
def write(slice : Bytes) : Int64
check_open

return 0u64 if slice.empty?
return 0i64 if slice.empty?

@stream.avail_in = slice.size
@stream.next_in = slice
consume_output LibZ::Flush::NO_FLUSH

slice.size.to_u64
slice.size.to_i64
end

# See `IO#flush`.
Expand Down
6 changes: 3 additions & 3 deletions src/compress/gzip/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class Compress::Gzip::Writer < IO
end

# See `IO#write`.
def write(slice : Bytes) : UInt64
def write(slice : Bytes) : Int64
check_open

return 0u64 if slice.empty?
return 0i64 if slice.empty?

flate_io = write_header
flate_io.write(slice)
Expand All @@ -83,7 +83,7 @@ class Compress::Gzip::Writer < IO
# uncompressed data size can be bigger.
@isize &+= slice.size

slice.size.to_u64
slice.size.to_i64
end

# Flushes data, forcing writing the gzip header if no
Expand Down
4 changes: 2 additions & 2 deletions src/compress/zip/checksum_writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module Compress::Zip
raise IO::Error.new "Can't read from Zip::Writer entry"
end

def write(slice : Bytes) : UInt64
return 0u64 if slice.empty?
def write(slice : Bytes) : Int64
return 0i64 if slice.empty?

@count += slice.size
@crc32 = Digest::CRC32.update(slice, @crc32) if @compute_crc32
Expand Down
6 changes: 3 additions & 3 deletions src/compress/zlib/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ class Compress::Zlib::Writer < IO
end

# See `IO#write`.
def write(slice : Bytes) : UInt64
def write(slice : Bytes) : Int64
check_open

return 0u64 if slice.empty?
return 0i64 if slice.empty?

write_header unless @wrote_header

@flate_io.write(slice)
@adler32 = Digest::Adler32.update(slice, @adler32)

slice.size.to_u64
slice.size.to_i64
end

# Flushes data, forcing writing the zlib header if no
Expand Down
2 changes: 1 addition & 1 deletion src/float.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct Float

# Writes this float to the given *io* in the given *format*.
# See also: `IO#write_bytes`.
def to_io(io : IO, format : IO::ByteFormat) : UInt64
def to_io(io : IO, format : IO::ByteFormat) : Int64
format.encode(self, io)
end

Expand Down
8 changes: 4 additions & 4 deletions src/http/content.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module HTTP
super
end

def skip(bytes_count : Int) : UInt64
def skip(bytes_count : Int) : Int64
ensure_send_continue
super
end
Expand Down Expand Up @@ -73,7 +73,7 @@ module HTTP
@io.peek
end

def skip(bytes_count : Int) : UInt64
def skip(bytes_count : Int) : Int64
ensure_send_continue
@io.skip(bytes_count)
end
Expand Down Expand Up @@ -164,8 +164,8 @@ module HTTP
peek
end

def skip(bytes_count : Int) : UInt64
bytes_count = bytes_count.to_u64
def skip(bytes_count : Int) : Int64
bytes_count = bytes_count.to_i64
ensure_send_continue

if bytes_count <= @chunk_remaining
Expand Down
4 changes: 2 additions & 2 deletions src/http/server/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class HTTP::Server
end

# See `IO#write(slice)`.
def write(slice : Bytes) : UInt64
return 0u64 if slice.empty?
def write(slice : Bytes) : Int64
return 0i64 if slice.empty?

@output.write(slice)
end
Expand Down
6 changes: 3 additions & 3 deletions src/http/web_socket/protocol.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class HTTP::WebSocket::Protocol
@pos = 0
end

def write(slice : Bytes) : UInt64
return 0u64 if slice.empty?
def write(slice : Bytes) : Int64
return 0i64 if slice.empty?

count = Math.min(@buffer.size - @pos, slice.size)
(@buffer + @pos).copy_from(slice.to_unsafe, count)
Expand All @@ -67,7 +67,7 @@ class HTTP::WebSocket::Protocol
write(slice + count)
end

slice.size.to_u64
slice.size.to_i64
end

def read(slice : Bytes)
Expand Down
2 changes: 1 addition & 1 deletion src/int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ struct Int
# Writes this integer to the given *io* in the given *format*.
#
# See also: `IO#write_bytes`.
def to_io(io : IO, format : IO::ByteFormat) : UInt64
def to_io(io : IO, format : IO::ByteFormat) : Int64
format.encode(self, io)
end

Expand Down
32 changes: 16 additions & 16 deletions src/io.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ require "c/errno"
# slice.size
# end
#
# def write(slice : Bytes) : UInt64
# def write(slice : Bytes) : Int64
# slice.size.times { |i| @slice[i] = slice[i] }
# @slice += slice.size
# slice.size.to_u64
# slice.size.to_i64
# end
# end
#
Expand Down Expand Up @@ -100,7 +100,7 @@ abstract class IO
# io.write(slice)
# io.to_s # => "abcd"
# ```
abstract def write(slice : Bytes) : UInt64
abstract def write(slice : Bytes) : Int64

# Closes this `IO`.
#
Expand Down Expand Up @@ -464,7 +464,7 @@ abstract class IO
end

# Writes a slice of UTF-8 encoded bytes to this `IO`, using the current encoding.
def write_utf8(slice : Bytes) : UInt64
def write_utf8(slice : Bytes) : Int64
if encoder = encoder()
encoder.write(self, slice)
else
Expand Down Expand Up @@ -812,8 +812,8 @@ abstract class IO
# io.gets # => "world"
# io.skip(1) # raises IO::EOFError
# ```
def skip(bytes_count : Int) : UInt64
bytes_count = bytes_count.to_u64
def skip(bytes_count : Int) : Int64
bytes_count = bytes_count.to_i64
remaining = bytes_count
buffer = uninitialized UInt8[4096]
while remaining > 0
Expand All @@ -826,8 +826,8 @@ abstract class IO

# Reads and discards bytes from `self` until there
# are no more bytes.
def skip_to_end : UInt64
bytes_count = 0_u64
def skip_to_end : Int64
bytes_count = 0i64
buffer = uninitialized UInt8[4096]
while (len = read(buffer.to_slice)) > 0
bytes_count &+= len
Expand All @@ -842,7 +842,7 @@ abstract class IO
# io.write_byte 97_u8
# io.to_s # => "a"
# ```
def write_byte(byte : UInt8) : UInt64
def write_byte(byte : UInt8) : Int64
x = byte
write Slice.new(pointerof(x), 1)
end
Expand All @@ -861,7 +861,7 @@ abstract class IO
# io.rewind
# io.gets(4) # => "\u{4}\u{3}\u{2}\u{1}"
# ```
def write_bytes(object, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) : UInt64
def write_bytes(object, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) : Int64
object.to_io(self, format)
end

Expand Down Expand Up @@ -1117,12 +1117,12 @@ abstract class IO
#
# io2.to_s # => "hello"
# ```
def self.copy(src, dst) : UInt64
def self.copy(src, dst) : Int64
buffer = uninitialized UInt8[4096]
count = 0_u64
count = 0_i64
while (len = src.read(buffer.to_slice).to_i32) > 0
dst.write buffer.to_slice[0, len]
count += len
count &+= len
end
count
end
Expand All @@ -1137,16 +1137,16 @@ abstract class IO
#
# io2.to_s # => "hel"
# ```
def self.copy(src, dst, limit : Int) : UInt64
def self.copy(src, dst, limit : Int) : Int64
raise ArgumentError.new("Negative limit") if limit < 0

limit = limit.to_u64
limit = limit.to_i64

buffer = uninitialized UInt8[4096]
remaining = limit
while (len = src.read(buffer.to_slice[0, Math.min(buffer.size, Math.max(remaining, 0))])) > 0
dst.write buffer.to_slice[0, len]
remaining -= len
remaining &-= len
end
limit - remaining
end
Expand Down
Loading

0 comments on commit 6380fa9

Please sign in to comment.