tur/capability
capability-passing typeclasses for controlled side-effect injection.
Since: Phase 16
FileSystem-type
(FileSystem-type)
return NULL; declares the FileSystem struct typedef for inline C blocks.
Since: Phase 16
make-FileSystem
(make-FileSystem [read_fn write_fn delete_fn list_fn])
construct a FileSystem capability from four function pointers.
| read_fn | (path buf buf_len out_len) -> int | |
| write_fn | (path data data_len) -> int | |
| delete_fn | (path) -> int | |
| list_fn | (path entries max_entries out_count) -> int |
A heap-allocated FileSystem capability pointer; free with FileSystem-free.
(def fs (make-FileSystem my-read my-write my-delete my-list))
Since: Phase 16
FileSystem-free
(FileSystem-free [fs])
free a FileSystem capability allocated by make-FileSystem.
| fs | FileSystem pointer from make-FileSystem |
Since: Phase 16
Logger-type
(Logger-type)
return NULL; declares the Logger struct typedef for inline C blocks.
Since: Phase 16
make-Logger
(make-Logger [debug_fn info_fn warn_fn error_fn])
construct a Logger capability from four function pointers (debug/info/warn/error).
| debug_fn | (msg: cstr) -> void | |
| info_fn | (msg: cstr) -> void | |
| warn_fn | (msg: cstr) -> void | |
| error_fn | (msg: cstr) -> void |
A heap-allocated Logger capability pointer; free with Logger-free.
Since: Phase 16
Logger-free
(Logger-free [log])
free a Logger capability allocated by make-Logger.
| log | Logger pointer from make-Logger |
Since: Phase 16
Random-type
(Random-type)
return NULL; declares the Random struct typedef for inline C blocks.
Since: Phase 16
make-Random
(make-Random [next_int_fn next_float_fn])
construct a Random capability from two function pointers.
| next_int_fn | (min max) -> int; returns integer in [min, max] | |
| next_float_fn | () -> int; returns value in [0, 10000) representing a float |
A heap-allocated Random capability pointer; free with Random-free.
Since: Phase 16
Random-free
(Random-free [rng])
free a Random capability allocated by make-Random.
| rng | Random pointer from make-Random |
Since: Phase 16
Time-type
(Time-type)
return NULL; declares the Time struct typedef for inline C blocks.
Since: Phase 16
make-Time
(make-Time [now_fn sleep_fn])
construct a Time capability from two function pointers.
| now_fn | () -> int; returns current time in milliseconds since epoch | |
| sleep_fn | (ms: int) -> void; sleeps for ms milliseconds |
A heap-allocated Time capability pointer; free with Time-free.
Since: Phase 16
Time-free
(Time-free [t])
free a Time capability allocated by make-Time.
| t | Time pointer from make-Time |
Since: Phase 16
with-capability
(with-capability [binding cap_expr body])
bind cap_expr to binding and evaluate body with that capability in scope.
| binding | name to bind the capability to | |
| cap_expr | expression producing the capability | |
| body | expression to evaluate with binding in scope |
(with-capability [fs (make-FileSystem ...)] (fs-read fs "data.txt"))
Since: Phase 16
default-capability
(default-capability [type_name])
return the default implementation for a capability type (currently always nil).
| type_name | symbolic name of the capability type (unused in v1) |
Since: Phase 16
fs-read
(fs-read [fs path buf buf_len])
call read_file on a FileSystem capability.
| fs | FileSystem capability pointer | |
| path | file path to read | |
| buf | output buffer pointer | |
| buf_len | size of buf in bytes |
0 on success, non-zero on error (as returned by the underlying read_file fn).
Since: Phase 16
fs-write
(fs-write [fs path data data_len])
call write_file on a FileSystem capability.
| fs | FileSystem capability pointer | |
| path | file path to write | |
| data | data buffer pointer | |
| data_len | number of bytes to write |
0 on success, non-zero on error.
Since: Phase 16
fs-delete
(fs-delete [fs path])
call delete on a FileSystem capability.
| fs | FileSystem capability pointer | |
| path | path of the file to delete |
0 on success, non-zero on error.
Since: Phase 16
fs-list
(fs-list [fs path entries max_entries])
call list on a FileSystem capability.
| fs | FileSystem capability pointer | |
| path | directory path to list | |
| entries | output buffer for entry name pointers | |
| max_entries | capacity of entries buffer |
Number of entries found, or negative on error.
Since: Phase 16
log-debug
(log-debug [log msg])
emit a debug-level log message via a Logger capability.
| log | Logger capability pointer | |
| msg | message string to log |
Since: Phase 16
log-info
(log-info [log msg])
emit an info-level log message via a Logger capability.
| log | Logger capability pointer | |
| msg | message string to log |
Since: Phase 16
log-warn
(log-warn [log msg])
emit a warning-level log message via a Logger capability.
| log | Logger capability pointer | |
| msg | message string to log |
Since: Phase 16
log-error
(log-error [log msg])
emit an error-level log message via a Logger capability.
| log | Logger capability pointer | |
| msg | message string to log |
Since: Phase 16
rng-next-int
(rng-next-int [rng min max])
return a random integer in [min, max] from a Random capability.
| rng | Random capability pointer | |
| min | inclusive lower bound | |
| max | inclusive upper bound |
A random integer in [min, max].
Since: Phase 16
rng-next-float
(rng-next-float [rng])
return a random pseudo-float in [0, 10000) from a Random capability.
| rng | Random capability pointer |
Integer in [0, 10000); divide by 10000.0 to get a float in [0.0, 1.0).
Since: Phase 16
time-now
(time-now [t])
return the current time in milliseconds since epoch from a Time capability.
| t | Time capability pointer |
Current time as an integer (milliseconds since epoch).
Since: Phase 16
time-sleep
(time-sleep [t ms])
sleep for ms milliseconds via a Time capability.
| t | Time capability pointer | |
| ms | duration in milliseconds |
Since: Phase 16