From 6f12322bdad67462a11b3a40ccc018116676d59b Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 13 Oct 2011 14:30:31 -0700 Subject: [PATCH] kill noncopying-reader mixin, stream-read generic Now that all streams have been updated to implement the stream-read-unsafe protocol, take out the noncopying-reader shim. Turn stream-read and stream-read-partial into plain functions over the -unsafe generics. --- basis/alien/data/data.factor | 1 - basis/io/ports/ports.factor | 1 - basis/io/streams/limited/limited.factor | 6 --- basis/io/streams/null/null.factor | 2 - basis/io/streams/string/string.factor | 1 - basis/io/streams/throwing/throwing.factor | 8 ---- basis/ui/tools/listener/listener.factor | 1 - core/io/encodings/encodings.factor | 1 - core/io/io.factor | 41 +++++++++----------- core/io/streams/byte-array/byte-array.factor | 1 - core/io/streams/c/c.factor | 2 - extra/io/streams/peek/peek.factor | 1 - 12 files changed, 18 insertions(+), 48 deletions(-) diff --git a/basis/alien/data/data.factor b/basis/alien/data/data.factor index ac7478d24a..7191dcc718 100644 --- a/basis/alien/data/data.factor +++ b/basis/alien/data/data.factor @@ -93,7 +93,6 @@ M: bad-byte-array-length summary : malloc-string ( string encoding -- alien ) string>alien malloc-byte-array ; -INSTANCE: memory-stream noncopying-reader M:: memory-stream stream-read-unsafe ( n buf stream -- count ) stream alien>> :> src buf src n memcpy diff --git a/basis/io/ports/ports.factor b/basis/io/ports/ports.factor index ea20622cb7..8f5ef977b9 100644 --- a/basis/io/ports/ports.factor +++ b/basis/io/ports/ports.factor @@ -29,7 +29,6 @@ TUPLE: buffered-port < port { buffer buffer } ; default-buffer-size get >>buffer ; inline TUPLE: input-port < buffered-port ; -INSTANCE: input-port noncopying-reader M: input-port stream-element-type drop +byte+ ; inline diff --git a/basis/io/streams/limited/limited.factor b/basis/io/streams/limited/limited.factor index 0ab44bca04..175c53663b 100644 --- a/basis/io/streams/limited/limited.factor +++ b/basis/io/streams/limited/limited.factor @@ -86,12 +86,6 @@ M: limited-stream stream-read1 1 swap [ nip stream-read1 ] maybe-read ; -M: limited-stream stream-read - [ stream-read ] maybe-read ; - -M: limited-stream stream-read-partial - [ stream-read-partial ] maybe-read ; - M: limited-stream stream-read-unsafe [ stream-read-unsafe ] maybe-read-unsafe ; diff --git a/basis/io/streams/null/null.factor b/basis/io/streams/null/null.factor index d47ff9b9f7..ae4fae2a90 100644 --- a/basis/io/streams/null/null.factor +++ b/basis/io/streams/null/null.factor @@ -13,8 +13,6 @@ M: null-reader stream-element-type drop +byte+ ; M: null-reader stream-readln drop f ; M: null-reader stream-read1 drop f ; M: null-reader stream-read-until 2drop f f ; -M: null-reader stream-read 2drop f ; -M: null-reader stream-read-partial 2drop f ; M: null-reader stream-read-unsafe 3drop 0 ; M: null-reader stream-read-partial-unsafe 3drop 0 ; diff --git a/basis/io/streams/string/string.factor b/basis/io/streams/string/string.factor index 3a3815471a..909fed2092 100644 --- a/basis/io/streams/string/string.factor +++ b/basis/io/streams/string/string.factor @@ -8,7 +8,6 @@ IN: io.streams.string ! Readers TUPLE: string-reader { underlying string read-only } { i array-capacity } ; -INSTANCE: string-reader noncopying-reader M: string-reader stream-element-type drop +character+ ; M: string-reader stream-read-partial-unsafe sequence-read-unsafe ; diff --git a/basis/io/streams/throwing/throwing.factor b/basis/io/streams/throwing/throwing.factor index 88891a5fed..4c8e5b74c7 100644 --- a/basis/io/streams/throwing/throwing.factor +++ b/basis/io/streams/throwing/throwing.factor @@ -20,14 +20,6 @@ M:: throws-on-eof-stream stream-read1 ( stream -- obj ) stream stream>> stream-read1 [ 1 stream \ read1 stream-exhausted ] unless* ; -M:: throws-on-eof-stream stream-read ( n stream -- seq ) - n stream stream>> stream-read - dup length n = [ n stream \ read stream-exhausted ] unless ; - -M:: throws-on-eof-stream stream-read-partial ( n stream -- seq ) - n stream stream>> stream-read-partial - [ n stream \ read-partial stream-exhausted ] unless* ; - M:: throws-on-eof-stream stream-read-unsafe ( n buf stream -- count ) n buf stream stream>> stream-read-unsafe dup n = [ n stream \ stream-read-unsafe stream-exhausted ] unless ; diff --git a/basis/ui/tools/listener/listener.factor b/basis/ui/tools/listener/listener.factor index f07116d7cb..2829c214e2 100644 --- a/basis/ui/tools/listener/listener.factor +++ b/basis/ui/tools/listener/listener.factor @@ -23,7 +23,6 @@ IN: ui.tools.listener ! evaluate-input resumes the thread. TUPLE: interactor < source-editor output history flag mailbox thread waiting token-model word-model popup ; -INSTANCE: interactor noncopying-reader : register-self ( interactor -- ) >>mailbox diff --git a/core/io/encodings/encodings.factor b/core/io/encodings/encodings.factor index b0244244ae..6750ad5c42 100644 --- a/core/io/encodings/encodings.factor +++ b/core/io/encodings/encodings.factor @@ -26,7 +26,6 @@ GENERIC: ( stream encoding -- newstream ) CONSTANT: replacement-char HEX: fffd TUPLE: decoder { stream read-only } { code read-only } { cr boolean } ; -INSTANCE: decoder noncopying-reader ERROR: decode-error ; diff --git a/core/io/io.factor b/core/io/io.factor index 93f8da0550..fda1c7ac0f 100644 --- a/core/io/io.factor +++ b/core/io/io.factor @@ -9,11 +9,9 @@ SYMBOLS: +byte+ +character+ ; GENERIC: stream-element-type ( stream -- type ) GENERIC: stream-read1 ( stream -- elt ) -GENERIC: stream-read-unsafe ( n buf stream -- n' ) -GENERIC: stream-read ( n stream -- seq ) +GENERIC: stream-read-unsafe ( n buf stream -- count ) GENERIC: stream-read-until ( seps stream -- seq sep/f ) -GENERIC: stream-read-partial-unsafe ( n buf stream -- n' ) -GENERIC: stream-read-partial ( n stream -- seq ) +GENERIC: stream-read-partial-unsafe ( n buf stream -- count ) GENERIC: stream-readln ( stream -- str/f ) GENERIC: stream-write1 ( elt stream -- ) @@ -37,9 +35,7 @@ SYMBOL: error-stream : readln ( -- str/f ) input-stream get stream-readln ; : read1 ( -- elt ) input-stream get stream-read1 ; -: read ( n -- seq ) input-stream get stream-read ; : read-until ( seps -- seq sep/f ) input-stream get stream-read-until ; -: read-partial ( n -- seq ) input-stream get stream-read-partial ; : tell-input ( -- n ) input-stream get stream-tell ; : tell-output ( -- n ) output-stream get stream-tell ; : seek-input ( n seek-type -- ) input-stream get stream-seek ; @@ -93,8 +89,24 @@ SYMBOL: error-stream { +character+ [ SBUF" " ] } } case ; inline +: (new-sequence-for-stream) ( n stream -- seq ) + stream-exemplar new-sequence ; inline + +: (read-into-new) ( n stream quot -- byte-array/f ) + [ 2dup (new-sequence-for-stream) swap ] dip curry keep + over 0 = [ 2drop f ] [ resize ] if ; inline + PRIVATE> +: stream-read ( n stream -- seq/f ) + [ stream-read-unsafe ] (read-into-new) ; inline + +: stream-read-partial ( n stream -- seq/f ) + [ stream-read-partial-unsafe ] (read-into-new) ; inline + +: read ( n -- seq ) input-stream get stream-read ; +: read-partial ( n -- seq ) input-stream get stream-read-partial ; + : each-stream-line ( ... stream quot: ( ... line -- ... ) -- ... ) swap [ stream-readln ] curry each-morsel ; inline @@ -137,20 +149,3 @@ PRIVATE> : stream-copy ( in out -- ) [ [ [ write ] each-block ] with-output-stream ] curry with-input-stream ; - -! Implement stream-read and stream-read-partial in terms of -unsafe - -MIXIN: noncopying-reader - -: (new-sequence-for-stream) ( n stream -- seq ) - stream-exemplar new-sequence ; inline - -: (read-into-new) ( n stream quot -- byte-array/f ) - [ 2dup (new-sequence-for-stream) swap ] dip curry keep - over 0 = [ 2drop f ] [ resize ] if ; inline - -M: noncopying-reader stream-read - [ stream-read-unsafe ] (read-into-new) ; inline - -M: noncopying-reader stream-read-partial - [ stream-read-partial-unsafe ] (read-into-new) ; inline diff --git a/core/io/streams/byte-array/byte-array.factor b/core/io/streams/byte-array/byte-array.factor index b917086206..89f838a675 100644 --- a/core/io/streams/byte-array/byte-array.factor +++ b/core/io/streams/byte-array/byte-array.factor @@ -15,7 +15,6 @@ M: byte-vector stream-element-type drop +byte+ ; dup encoder? [ stream>> ] when >byte-array ; inline TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ; -INSTANCE: byte-reader noncopying-reader M: byte-reader stream-element-type drop +byte+ ; diff --git a/core/io/streams/c/c.factor b/core/io/streams/c/c.factor index c6ee03eaa7..ac02132bf5 100644 --- a/core/io/streams/c/c.factor +++ b/core/io/streams/c/c.factor @@ -40,7 +40,6 @@ M: c-writer stream-write M: c-writer stream-flush dup check-disposed handle>> fflush ; TUPLE: c-reader < c-stream ; -INSTANCE: c-reader noncopying-reader : ( handle -- stream ) c-reader new-c-stream ; @@ -49,7 +48,6 @@ M: c-reader stream-element-type drop +byte+ ; M: c-reader stream-read-unsafe dup check-disposed handle>> fread-unsafe ; M: c-reader stream-read-partial-unsafe stream-read-unsafe ; -M: c-reader stream-read-partial stream-read ; M: c-reader stream-read1 dup check-disposed handle>> fgetc ; diff --git a/extra/io/streams/peek/peek.factor b/extra/io/streams/peek/peek.factor index a1dab9a2a3..2137a88383 100644 --- a/extra/io/streams/peek/peek.factor +++ b/extra/io/streams/peek/peek.factor @@ -6,7 +6,6 @@ vectors ; IN: io.streams.peek TUPLE: peek-stream stream peeked ; -INSTANCE: peek-stream noncopying-reader M: peek-stream dispose stream>> dispose ;