Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions spec/std/io/null_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "spec"

describe IO::Null do
it "#read" do
slice = Bytes.new(10, 1_u8)
io = IO::Null.new

io.read(slice).should eq(0)
slice.each { |byte| byte.should eq(1_u8) }
end

it "#write" do
io = IO::Null.new
io.write Bytes.new(10)
end
end
17 changes: 17 additions & 0 deletions src/io/null.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# An IO object that does nothing, yet always succeeds and never fails.
#
# Any attempt to read results in end-of-file (EOF). Any data written is
# immediately discarded.
#
# Can be used in place of another IO when there is nothing to read, or when the
# output can be discarded.
class IO::Null < IO
# Always returns 0 (reached EOF).
def read(slice : Bytes)
0
end

# Returns immediately (discards *slice*).
def write(slice : Bytes) : Nil
end
end
Loading