diff --git a/collects/scheme/private/serialize.ss b/collects/scheme/private/serialize.ss index ee49cbf38f..fd9a41221b 100644 --- a/collects/scheme/private/serialize.ss +++ b/collects/scheme/private/serialize.ss @@ -349,7 +349,7 @@ (deserial-shell (unbox v) mod-map (not-ready-fixup sv) n) (deserialize-one v share mod-map))]) (vector-set! share n val) - v) + val) sv))) (define (deserialize-one v share mod-map) @@ -426,7 +426,7 @@ v0)] [(h) ;; Hash table - (let ([ht0 (make-hash-table)]) + (let ([ht0 (apply make-hash-table (cdr v))]) (vector-set! fixup n (lambda (ht) (hash-table-for-each ht diff --git a/collects/scribble/eval.ss b/collects/scribble/eval.ss index dcb093ed60..5cec7e40d6 100644 --- a/collects/scribble/eval.ss +++ b/collects/scribble/eval.ss @@ -209,7 +209,7 @@ [sandbox-error-output 'string] [sandbox-eval-limits #f] [sandbox-make-inspector current-inspector]) - (make-evaluator 'scheme/base))) + (make-evaluator '(begin (require scheme/base))))) (define (do-plain-eval ev s catching-exns?) (call-with-values (lambda () @@ -223,6 +223,8 @@ [(module . _rest) (syntax->datum s)] [_else s])] + [(bytes? s) + `(begin ,s)] [(string? s) `(begin ,s)] [else s])))) diff --git a/collects/scribblings/guide/io.scrbl b/collects/scribblings/guide/io.scrbl index 49eacb7d1b..6b54d25576 100644 --- a/collects/scribblings/guide/io.scrbl +++ b/collects/scribblings/guide/io.scrbl @@ -1,9 +1,11 @@ #lang scribble/doc -@require[scribble/manual] -@require[scribble/struct] -@require[scribble/eval] -@require[mzlib/process] -@require["guide-utils.ss"] +@(require scribble/manual + scribble/struct + scribble/eval + mzlib/process + "guide-utils.ss" + (for-label scheme/tcp + scheme/serialize)) @(define io-eval (make-base-eval)) @@ -51,7 +53,17 @@ examples: (close-input-port in) ] -@interaction-eval[#:eval io-eval (when (file-exists? "data") (delete-file "data"))] +If a file exists already, then @scheme[open-output-file] raises an +exception by default. Supply an option like @scheme[#:exists +'truncate] or @scheme[#:exists 'update] to re-write or update the +file: + +@examples[ +#:eval io-eval +(define out (open-output-file "data" #:exists 'truncate)) +(display "howdy" out) +(close-output-port out) +] Instead of having to match @scheme[open-input-file] and @scheme[open-output-file] calls, most Scheme programmers will instead @@ -61,6 +73,7 @@ with the output port; when the function returns, the port is closed. @examples[ #:eval io-eval (call-with-output-file "data" + #:exists 'truncate (lambda (out) (display "hello" out))) (call-with-input-file "data" @@ -107,7 +120,7 @@ with the output port; when the function returns, the port is closed. @;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @item{@bold{Process Pipes:} The @scheme[process] function runs a new + @item{@bold{Process Pipes:} The @scheme[subprocess] function runs a new process at the OS level and returns ports that correspond to the subprocess's stdin, stdout, and stderr. (The first three arguments can be certain kinds of existing ports to connect directly to the @@ -207,6 +220,8 @@ ways to print an instance of a built-in value: } +Here are some examples using each: + @twocolumn[ @interaction[ @@ -243,8 +258,8 @@ text. In the format string supplied to @scheme[printf], @litchar{~a} (deliver "John" "string") ] -An advantage of @scheme[write] is that many forms of data can be -read back in using @scheme[read]. +An advantage of @scheme[write], as opposed to @scheme[display], is +that many forms of data can be read back in using @scheme[read]. @examples[ #:eval io-eval @@ -258,8 +273,56 @@ read back in using @scheme[read]. ] @; ---------------------------------------------------------------------- -@subsection{Serialization} +@section{Datatypes and Serialization} +New datatypes created by @scheme[define-struct] by default +@scheme[write] either using @schemeresultfont{#<....>} notation (for +opaque structure types) or using @schemeresultfont{#(....)} vector +notation (for transparent structure types). In neither can can the +result be read back in as an instance of the structure type. + +@examples[ +(define-struct posn (x y)) +(write (make-posn 1 2)) +(define-values (in out) (make-pipe)) +(write (make-posn 1 2) out) +(read in) +] + +@interaction[ +(define-struct posn (x y) #:transparent) +(write (make-posn 1 2)) +(define-values (in out) (make-pipe)) +(write (make-posn 1 2) out) +(define v (read in)) +v +(posn? v) +(vector? v) +] + +The @scheme[define-serializable-struct] form defines a structure type +that can be @scheme[serialize]d to a value that can be printed using +@scheme[write] and restored via @scheme[read]. The @scheme[serialize]d +result can be @scheme[deserialize]d to get back an instance of the +original structure type. The serialization form an functions are +provided by the @schememodname[scheme/serialize] library. + +@examples[ +(require scheme/serialize) +(define-serializable-struct posn (x y) #:transparent) +(deserialize (serialize (make-posn 1 2))) +(write (serialize (make-posn 1 2))) +(define-values (in out) (make-pipe)) +(write (serialize (make-posn 1 2)) out) +(deserialize (read in)) +] + +In addition to the names bound by @scheme[define-struct], +@scheme[define-serializable-struct] binds an identifier with +deserialization information, and it automatically @scheme[provide]s +the deserialization identifier from a module context. This +deserialization identifier is accessed reflectively when a value is +deserialized. @; ---------------------------------------------------------------------- @section{Bytes versus Characters} diff --git a/collects/scribblings/inside/utils.ss b/collects/scribblings/inside/utils.ss index 9354de5c22..623d199e8d 100644 --- a/collects/scribblings/inside/utils.ss +++ b/collects/scribblings/inside/utils.ss @@ -148,7 +148,7 @@ [more (apply tt more)])) (define cppi cpp) -(define cppdef (lambda (x) (as-cpp-defn x (as-index (cpp x))))) +(define cppdef (lambda (x) (as-cpp-defn x (cpp x)))) (define *var italic) (define mzc (exec "mzc")) diff --git a/collects/scribblings/reference/write.scrbl b/collects/scribblings/reference/write.scrbl index 415fefa729..c2247cefca 100644 --- a/collects/scribblings/reference/write.scrbl +++ b/collects/scribblings/reference/write.scrbl @@ -60,7 +60,7 @@ escapes: @itemize{ - @item{@FmtMark{n} or @FmtMark{%} prints a newline} + @item{@FmtMark{n} or @FmtMark{%} prints a newline, the same as @litchar{\n}} @item{@FmtMark{a} or @FmtMark{A} @scheme[display]s the next argument among the @scheme[v]s} @@ -92,12 +92,13 @@ escapes: @item{@FmtMark{~} prints a tilde.} - @item{@FmtMark{}@nonterm{w}, where @nonterm{w} is a whitespace character, - skips characters in @scheme[form] until a non-whitespace - character is encountered or until a second end-of-line is - encountered (whichever happens first). An end-of-line is either - @scheme[#\return], @scheme[#\newline], or @scheme[#\return] followed - immediately by @scheme[#\newline] (on all platforms).} + @item{@FmtMark{}@nonterm{w}, where @nonterm{w} is a whitespace + character (see @scheme[char-whitespace?]), skips characters in + @scheme[form] until a non-whitespace character is encountered or + until a second end-of-line is encountered (whichever happens + first). On all platforms, an end-of-line can be @scheme[#\return], + @scheme[#\newline], or @scheme[#\return] followed immediately by + @scheme[#\newline].} }