Last active
August 29, 2015 14:16
-
-
Save ktakashi/7fa25f74189a8af509dc to your computer and use it in GitHub Desktop.
Returning value of custom binary output port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!r6rs | |
(import (rnrs)) | |
(define (print . args) (for-each display args) (newline)) | |
(define (make-predefined-buffer-output-port buf) | |
(define left (bytevector-length buf)) | |
(define index 0) | |
(define (write! bv start count) | |
(cond ((zero? left) 0) | |
((> count left) | |
(let ((size left)) | |
(bytevector-copy! bv start buf index size) | |
(set! index (+ index left)) | |
(set! left 0) | |
size)) | |
(else | |
(bytevector-copy! bv start buf index count) | |
(set! index (+ index count)) | |
(set! left (- left count)) | |
count))) | |
(define (close!) #t) | |
(define (get-position) index) | |
(make-custom-binary-output-port "predefined buffer port" | |
write! get-position #f close!)) | |
(define buf (make-bytevector 1)) | |
(define out (make-predefined-buffer-output-port buf)) | |
(print (put-bytevector out #vu8(1 2 3))) | |
(flush-output-port out) | |
(print buf) | |
(flush-output-port (current-output-port)) | |
#| | |
;; Updated to support port-position | |
- Mosh and Sagittarius print #vu8(1) | |
- Vicare went into inifinite loop | |
- Racket (v6.1.1) raised an error (even port-position is supported) | |
- Guile also goes to infinte loop (picked up from the comment) | |
|# |
Here is my final take on this:
Scenario: In the case where not bytes written is less than count
.
If get-position
is #f
, then an error should be raised like it does with Racket.
If get-position
is not #f
, the procedure calling write!
should check if get-position
has advanced the same amount as the value returned from write!
. If not, raise an error.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
weinholt: I thought about it too, perhaps
put-XXX
should have been defined more specifically. Strictly speaking, an error should be the most desirable behavior. Getting stuck in an infinite loop seems bad though :) I will try implementput-XXX
to behave, alternatively, get them to return the actual number of bytes/characters written.