io, io.streams.c: factor noncopying-reader mixin
Instances of the mixin implement stream-read and stream-read-partial in terms of stream-read-unsafe and stream-read-partial-unsafe, respectively.db4
parent
2518f1c0e9
commit
66bb07bbcf
|
@ -1,7 +1,7 @@
|
|||
! Copyright (C) 2003, 2010 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: accessors combinators continuations destructors kernel
|
||||
math namespaces sequences ;
|
||||
USING: accessors byte-arrays combinators continuations destructors
|
||||
kernel math namespaces sequences sequences.private ;
|
||||
IN: io
|
||||
|
||||
SYMBOLS: +byte+ +character+ ;
|
||||
|
@ -122,3 +122,20 @@ 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-element-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
|
||||
|
|
|
@ -40,16 +40,13 @@ 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
|
||||
|
||||
: <c-reader> ( handle -- stream ) c-reader new-c-stream ;
|
||||
|
||||
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
|
||||
[ dup <byte-array> ] dip
|
||||
[ stream-read-unsafe ] curry keep
|
||||
over 0 = [ 2drop f ] [ resize-byte-array ] if ;
|
||||
|
||||
M: c-reader stream-read-partial-unsafe stream-read-unsafe ;
|
||||
M: c-reader stream-read-partial stream-read ;
|
||||
|
|
Loading…
Reference in New Issue