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.
db4
Joe Groff 2011-10-13 14:30:31 -07:00
parent cd00603e2d
commit 6f12322bda
12 changed files with 18 additions and 48 deletions

View File

@ -93,7 +93,6 @@ M: bad-byte-array-length summary
: malloc-string ( string encoding -- alien ) : malloc-string ( string encoding -- alien )
string>alien malloc-byte-array ; string>alien malloc-byte-array ;
INSTANCE: memory-stream noncopying-reader
M:: memory-stream stream-read-unsafe ( n buf stream -- count ) M:: memory-stream stream-read-unsafe ( n buf stream -- count )
stream alien>> :> src stream alien>> :> src
buf src n memcpy buf src n memcpy

View File

@ -29,7 +29,6 @@ TUPLE: buffered-port < port { buffer buffer } ;
default-buffer-size get <buffer> >>buffer ; inline default-buffer-size get <buffer> >>buffer ; inline
TUPLE: input-port < buffered-port ; TUPLE: input-port < buffered-port ;
INSTANCE: input-port noncopying-reader
M: input-port stream-element-type drop +byte+ ; inline M: input-port stream-element-type drop +byte+ ; inline

View File

@ -86,12 +86,6 @@ M: limited-stream stream-read1
1 swap 1 swap
[ nip stream-read1 ] maybe-read ; [ 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 M: limited-stream stream-read-unsafe
[ stream-read-unsafe ] maybe-read-unsafe ; [ stream-read-unsafe ] maybe-read-unsafe ;

View File

@ -13,8 +13,6 @@ M: null-reader stream-element-type drop +byte+ ;
M: null-reader stream-readln drop f ; M: null-reader stream-readln drop f ;
M: null-reader stream-read1 drop f ; M: null-reader stream-read1 drop f ;
M: null-reader stream-read-until 2drop f 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-unsafe 3drop 0 ;
M: null-reader stream-read-partial-unsafe 3drop 0 ; M: null-reader stream-read-partial-unsafe 3drop 0 ;

View File

@ -8,7 +8,6 @@ IN: io.streams.string
! Readers ! Readers
TUPLE: string-reader { underlying string read-only } { i array-capacity } ; 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-element-type drop +character+ ;
M: string-reader stream-read-partial-unsafe sequence-read-unsafe ; M: string-reader stream-read-partial-unsafe sequence-read-unsafe ;

View File

@ -20,14 +20,6 @@ M:: throws-on-eof-stream stream-read1 ( stream -- obj )
stream stream>> stream-read1 stream stream>> stream-read1
[ 1 stream \ read1 stream-exhausted ] unless* ; [ 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 ) M:: throws-on-eof-stream stream-read-unsafe ( n buf stream -- count )
n buf stream stream>> stream-read-unsafe n buf stream stream>> stream-read-unsafe
dup n = [ n stream \ stream-read-unsafe stream-exhausted ] unless ; dup n = [ n stream \ stream-read-unsafe stream-exhausted ] unless ;

View File

@ -23,7 +23,6 @@ IN: ui.tools.listener
! evaluate-input resumes the thread. ! evaluate-input resumes the thread.
TUPLE: interactor < source-editor TUPLE: interactor < source-editor
output history flag mailbox thread waiting token-model word-model popup ; output history flag mailbox thread waiting token-model word-model popup ;
INSTANCE: interactor noncopying-reader
: register-self ( interactor -- ) : register-self ( interactor -- )
<mailbox> >>mailbox <mailbox> >>mailbox

View File

@ -26,7 +26,6 @@ GENERIC: <decoder> ( stream encoding -- newstream )
CONSTANT: replacement-char HEX: fffd CONSTANT: replacement-char HEX: fffd
TUPLE: decoder { stream read-only } { code read-only } { cr boolean } ; TUPLE: decoder { stream read-only } { code read-only } { cr boolean } ;
INSTANCE: decoder noncopying-reader
ERROR: decode-error ; ERROR: decode-error ;

View File

@ -9,11 +9,9 @@ SYMBOLS: +byte+ +character+ ;
GENERIC: stream-element-type ( stream -- type ) GENERIC: stream-element-type ( stream -- type )
GENERIC: stream-read1 ( stream -- elt ) GENERIC: stream-read1 ( stream -- elt )
GENERIC: stream-read-unsafe ( n buf stream -- n' ) GENERIC: stream-read-unsafe ( n buf stream -- count )
GENERIC: stream-read ( n stream -- seq )
GENERIC: stream-read-until ( seps stream -- seq sep/f ) GENERIC: stream-read-until ( seps stream -- seq sep/f )
GENERIC: stream-read-partial-unsafe ( n buf stream -- n' ) GENERIC: stream-read-partial-unsafe ( n buf stream -- count )
GENERIC: stream-read-partial ( n stream -- seq )
GENERIC: stream-readln ( stream -- str/f ) GENERIC: stream-readln ( stream -- str/f )
GENERIC: stream-write1 ( elt stream -- ) GENERIC: stream-write1 ( elt stream -- )
@ -37,9 +35,7 @@ SYMBOL: error-stream
: readln ( -- str/f ) input-stream get stream-readln ; : readln ( -- str/f ) input-stream get stream-readln ;
: read1 ( -- elt ) input-stream get stream-read1 ; : 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-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-input ( -- n ) input-stream get stream-tell ;
: tell-output ( -- n ) output-stream get stream-tell ; : tell-output ( -- n ) output-stream get stream-tell ;
: seek-input ( n seek-type -- ) input-stream get stream-seek ; : seek-input ( n seek-type -- ) input-stream get stream-seek ;
@ -93,8 +89,24 @@ SYMBOL: error-stream
{ +character+ [ SBUF" " ] } { +character+ [ SBUF" " ] }
} case ; inline } 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> 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 -- ... ) -- ... ) : each-stream-line ( ... stream quot: ( ... line -- ... ) -- ... )
swap [ stream-readln ] curry each-morsel ; inline swap [ stream-readln ] curry each-morsel ; inline
@ -137,20 +149,3 @@ PRIVATE>
: stream-copy ( in out -- ) : stream-copy ( in out -- )
[ [ [ write ] each-block ] with-output-stream ] [ [ [ write ] each-block ] with-output-stream ]
curry with-input-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

View File

@ -15,7 +15,6 @@ M: byte-vector stream-element-type drop +byte+ ;
dup encoder? [ stream>> ] when >byte-array ; inline dup encoder? [ stream>> ] when >byte-array ; inline
TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ; TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ;
INSTANCE: byte-reader noncopying-reader
M: byte-reader stream-element-type drop +byte+ ; M: byte-reader stream-element-type drop +byte+ ;

View File

@ -40,7 +40,6 @@ M: c-writer stream-write
M: c-writer stream-flush dup check-disposed handle>> fflush ; M: c-writer stream-flush dup check-disposed handle>> fflush ;
TUPLE: c-reader < c-stream ; TUPLE: c-reader < c-stream ;
INSTANCE: c-reader noncopying-reader
: <c-reader> ( handle -- stream ) c-reader new-c-stream ; : <c-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-unsafe dup check-disposed handle>> fread-unsafe ;
M: c-reader stream-read-partial-unsafe stream-read-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 ; M: c-reader stream-read1 dup check-disposed handle>> fgetc ;

View File

@ -6,7 +6,6 @@ vectors ;
IN: io.streams.peek IN: io.streams.peek
TUPLE: peek-stream stream peeked ; TUPLE: peek-stream stream peeked ;
INSTANCE: peek-stream noncopying-reader
M: peek-stream dispose stream>> dispose ; M: peek-stream dispose stream>> dispose ;