4 Batteries-Included Disposables
The disposable library includes a few extra modules that provide specialized disposables for various purposes.
4.1 Filesystem Disposables
(require disposable/file) | package: disposable |
procedure
(disposable-file [ #:contents contents #:parent-dir parent-dir]) → (disposable/c path-string?) contents : string? = "" parent-dir : path-string? = (find-system-dir 'temp-dir)
> (with-disposable ([tmpfile (disposable-file #:contents "foo")]) (printf "Created temporary file ~a\n" tmpfile) (printf "Contents = ~a\n" (file->string tmpfile)))
Created temporary file /var/tmp/rkttmp15884897851588489785406
Contents = foo
value
procedure
(disposable-directory [#:parent-dir parent-dir])
→ (disposable/c path-string?) parent-dir : path-string? = (find-system-dir 'temp-dir)
> (with-disposable ([tmpdir (disposable-directory)]) (with-disposable ([child (disposable-file #:parent-dir tmpdir)]) (printf "File = ~a\n" child) (printf "Directory children = ~a\n" (directory-list tmpdir))))
File = /var/tmp/rkttmp15884897861588489786103/rkttmp15884897861588489786103
Directory children = (rkttmp15884897861588489786103)
4.2 Utilities for Testing Disposables
(require disposable/testing) | package: disposable |
This module provides utilities for testing operations involving disposables. The bindings provided here are designed for testing purposes only, and are not intended for use in production environments.
procedure
(sequence->disposable seq) → disposable?
seq : sequence?
> (define abc-disp (sequence->disposable '(a b c)))
> (with-disposable ([item abc-disp]) (printf "Acquired ~v\n" item)) Acquired 'a
> (with-disposable ([item abc-disp]) (printf "Acquired ~v\n" item)) Acquired 'b
procedure
(disposable/event-log disp)
→ (disposable/c (list/c disposable? event-log?)) disp : disposable?
> (define ex/log (disposable/event-log example-disposable))
> (with-disposable ([ex+log-list ex/log]) (define ex (first ex+log-list)) (define elog (second ex+log-list)) (with-disposable ([n ex]) (printf "Acquired ~v\n" n)) (with-disposable ([n ex]) (printf "Acquired ~v\n" n)) (event-log-events elog))
Allocated 29
Acquired 29
Deallocated 29
Allocated 61
Acquired 61
Deallocated 61
'((alloc 29) (dealloc 29) (alloc 61) (dealloc 61))
procedure
(event-log? v) → boolean?
v : any/c
procedure
(event-log-events elog)
→ (listof (list/c (or/c 'alloc 'dealloc) any/c)) elog : event-log?
4.3 Example Disposables
(require disposable/example) | package: disposable |
Resource allocation can be tricky to showcase in documentation, so the disposable/example module provides a simple example-disposable that "allocates" a random number while printing a message to current-output-port during allocation and deallocation. This is used to help users visualize the timeline of disposable values when reading documentation. It is not considered a stable part of disposable, and is only documented to provide hyperlinks at use sites.
value
example-disposable : (disposable/c (integer-in 0 99))