From 2772ed8bfe108b66b7493bc5cb0c40ddeb1ca57d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 5 Nov 2010 09:01:08 +0800 Subject: prefer symbolic names for waiting read/writability There's no point in using constants that point to symbols instead of just the symbols themselves. --- README | 2 +- ext/kgio/connect.c | 6 +++--- ext/kgio/read_write.c | 16 ++++++++-------- lib/kgio.rb | 6 +++--- test/lib_read_write.rb | 20 ++++++++++---------- test/test_pipe_popen.rb | 2 +- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/README b/README index 70a7997..f8f4d56 100644 --- a/README +++ b/README @@ -8,7 +8,7 @@ applications. == Features * Can avoid expensive exceptions on common EAGAIN/EINPROGRESS errors, - returning Kgio::WaitReadable or Kgio::WaitWritable instead. + returning :wait_readable or :wait_writable instead. These exceptions got more expensive to hit under Ruby 1.9.2 (but should be fixed in Ruby 1.9.3 to 1.9.1 performance levels) diff --git a/ext/kgio/connect.c b/ext/kgio/connect.c index cd9b631..8defa7e 100644 --- a/ext/kgio/connect.c +++ b/ext/kgio/connect.c @@ -100,7 +100,7 @@ static VALUE kgio_tcp_connect(VALUE klass, VALUE ip, VALUE port) * Creates a new Kgio::TCPSocket object and initiates a * non-blocking connection. The caller should select/poll * on the socket for writability before attempting to write - * or optimistically attempt a write and handle Kgio::WaitWritable + * or optimistically attempt a write and handle :wait_writable * or Errno::EAGAIN. * * Unlike the TCPSocket.new in Ruby, this does NOT perform DNS @@ -153,7 +153,7 @@ static VALUE kgio_unix_connect(VALUE klass, VALUE path) * Creates a new Kgio::UNIXSocket object and initiates a * non-blocking connection. The caller should select/poll * on the socket for writability before attempting to write - * or optimistically attempt a write and handle Kgio::WaitWritable + * or optimistically attempt a write and handle :wait_writable * or Errno::EAGAIN. */ static VALUE kgio_unix_start(VALUE klass, VALUE path) @@ -215,7 +215,7 @@ static VALUE kgio_connect(VALUE klass, VALUE addr) * Creates a generic Kgio::Socket object and initiates a * non-blocking connection. The caller should select/poll * on the socket for writability before attempting to write - * or optimistically attempt a write and handle Kgio::WaitWritable + * or optimistically attempt a write and handle :wait_writable * or Errno::EAGAIN. */ static VALUE kgio_start(VALUE klass, VALUE addr) diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c index 890392c..13369ba 100644 --- a/ext/kgio/read_write.c +++ b/ext/kgio/read_write.c @@ -1,5 +1,5 @@ #include "kgio.h" -static VALUE mKgio_WaitReadable, mKgio_WaitWritable; +static VALUE sym_wait_readable, sym_wait_writable; static VALUE eErrno_EPIPE, eErrno_ECONNRESET; /* @@ -74,7 +74,7 @@ static int read_check(struct io_args *a, long n, const char *msg, int io_wait) a->ptr = RSTRING_PTR(a->buf); return -1; } else { - a->buf = mKgio_WaitReadable; + a->buf = sym_wait_readable; return 0; } } @@ -150,7 +150,7 @@ static VALUE kgio_read_bang(int argc, VALUE *argv, VALUE io) * * Returns nil on EOF. * - * Returns Kgio::WaitReadable if EAGAIN is encountered. + * Returns :wait_readable if EAGAIN is encountered. */ static VALUE kgio_tryread(int argc, VALUE *argv, VALUE io) { @@ -243,7 +243,7 @@ done: } else if (written > 0) { a->buf = rb_str_new(a->ptr, a->len); } else { - a->buf = mKgio_WaitWritable; + a->buf = sym_wait_writable; } return 0; } @@ -290,14 +290,14 @@ static VALUE kgio_write(VALUE io, VALUE str) /* * call-seq: * - * io.kgio_trywrite(str) -> nil or Kgio::WaitWritable + * io.kgio_trywrite(str) -> nil or :wait_writable * * Returns nil if the write was completed in full. * * Returns a String containing the unwritten portion if EAGAIN * was encountered, but some portion was successfully written. * - * Returns Kgio::WaitWritable if EAGAIN is encountered and nothing + * Returns :wait_writable if EAGAIN is encountered and nothing * was written. */ static VALUE kgio_trywrite(VALUE io, VALUE str) @@ -353,8 +353,8 @@ void init_kgio_read_write(void) VALUE mPipeMethods, mSocketMethods; VALUE mKgio = rb_define_module("Kgio"); - mKgio_WaitReadable = rb_const_get(mKgio, rb_intern("WaitReadable")); - mKgio_WaitWritable = rb_const_get(mKgio, rb_intern("WaitWritable")); + sym_wait_readable = ID2SYM(rb_intern("wait_readable")); + sym_wait_writable = ID2SYM(rb_intern("wait_writable")); /* * Document-module: Kgio::PipeMethods diff --git a/lib/kgio.rb b/lib/kgio.rb index 2ff366e..0d28d4e 100644 --- a/lib/kgio.rb +++ b/lib/kgio.rb @@ -8,11 +8,11 @@ module Kgio LOCALHOST = '127.0.0.1' # Kgio::PipeMethods#kgio_tryread and Kgio::SocketMethods#kgio_tryread will - # return this constant when waiting for a read is required. + # return :wait_readable when waiting for a read is required. WaitReadable = :wait_readable - # PipeMethods#kgio_trywrite and SocketMethods#kgio_trywrite will - # return this constant when waiting for a read is required. + # PipeMethods#kgio_trywrite and SocketMethods#kgio_trywrite will return + # :wait_writable when waiting for a read is required. WaitWritable = :wait_writable end diff --git a/test/lib_read_write.rb b/test/lib_read_write.rb index 6f90041..c11b3af 100644 --- a/test/lib_read_write.rb +++ b/test/lib_read_write.rb @@ -97,9 +97,9 @@ module LibReadWriteTest case rv when String wr = rv - when Kgio::WaitReadable + when :wait_readable assert false, "should never get here line=#{__LINE__}" - when Kgio::WaitWritable + when :wait_writable IO.select(nil, [ @wr ]) else wr = false @@ -122,7 +122,7 @@ module LibReadWriteTest end def test_tryread_empty - assert_equal Kgio::WaitReadable, @rd.kgio_tryread(1) + assert_equal :wait_readable, @rd.kgio_tryread(1) end def test_read_too_much @@ -159,10 +159,10 @@ module LibReadWriteTest def test_trywrite_return_wait_writable tmp = [] - tmp << @wr.kgio_trywrite("HI") until tmp[-1] == Kgio::WaitWritable - assert Kgio::WaitWritable === tmp[-1] - assert(!(Kgio::WaitReadable === tmp[-1])) - assert_equal Kgio::WaitWritable, tmp.pop + tmp << @wr.kgio_trywrite("HI") until tmp[-1] == :wait_writable + assert :wait_writable === tmp[-1] + assert(!(:wait_readable === tmp[-1])) + assert_equal :wait_writable, tmp.pop assert tmp.size > 0 penultimate = tmp.pop assert(penultimate == "I" || penultimate == nil) @@ -173,7 +173,7 @@ module LibReadWriteTest def test_tryread_extra_buf_eagain_clears_buffer tmp = "hello world" rv = @rd.kgio_tryread(2, tmp) - assert_equal Kgio::WaitReadable, rv + assert_equal :wait_readable, rv assert_equal "", tmp end @@ -281,7 +281,7 @@ module LibReadWriteTest raise "Hello" end assert_nothing_raised { Kgio.wait_readable = :moo } - assert_equal Kgio::WaitReadable, @rd.kgio_tryread(5) + assert_equal :wait_readable, @rd.kgio_tryread(5) end def test_trywrite_wait_readable_method @@ -292,7 +292,7 @@ module LibReadWriteTest tmp = [] buf = "." * 1024 10000.times { tmp << @wr.kgio_trywrite(buf) } - assert_equal Kgio::WaitWritable, tmp.pop + assert_equal :wait_writable, tmp.pop end def test_wait_writable_method diff --git a/test/test_pipe_popen.rb b/test/test_pipe_popen.rb index 8d1e414..bfd18be 100644 --- a/test/test_pipe_popen.rb +++ b/test/test_pipe_popen.rb @@ -6,7 +6,7 @@ require 'kgio' class TestPipePopen < Test::Unit::TestCase def test_popen io = Kgio::Pipe.popen("sleep 1 && echo HI") - assert_equal Kgio::WaitReadable, io.kgio_tryread(2) + assert_equal :wait_readable, io.kgio_tryread(2) sleep 1.5 assert_equal "HI\n", io.kgio_read(3) assert_nil io.kgio_read(5) -- cgit v1.2.3-24-ge0c7