slice.ss
#lang scheme/base

(define-struct slice (bytes start end) #:transparent)

(define (create (num 0))
  (make-slice (bytes num) 0 num))

(define (from-bytes bytes)
  (make-slice bytes 0 (bytes-length bytes)))

(define (subslice slice start end)
  (make-slice (slice-bytes slice) start end))

(define (to-bytes slice)
  (subbytes (slice-bytes slice) (slice-start slice) (slice-end slice)))

(define (write-slice slice (output (current-output-port)))
  (write-bytes (slice-bytes slice) output
               (slice-start slice)
               (slice-end slice)))

(define (read-slice num (input (current-input-port)))
  (from-bytes (read-bytes num input)))

(define (read-slice! slice (input (current-input-port)))
  (read-bytes (slice-bytes slice) input 
              (slice-start slice)
              (slice-end slice)))

(provide
 (rename-out
  (read-slice read)
  (read-slice! read!)
  (write-slice write))
 create
 from-bytes
 slice?
; to-bytes should never need to use this.
 subslice)