Files
uni-channel/README.md
T
2026-07-06 15:17:43 +02:00

1.6 KiB

uni-channel

uni-channel provides one simple wrapper over async-channel, place-channel and port-channel.

(make-uni-channel channel)

The constructor detects the concrete channel kind and returns a uni-channel?. No helper constructors are part of the public API; create the underlying channel with the normal Racket or port-channel constructor and then wrap it.

A wrapped channel that can receive is itself synchronizable. (sync ch) returns the value received from the underlying channel. For an output-only endpoint, such as an output port-channel, the receive event is never-evt because no value can be received from that endpoint.

(require racket/async-channel
         uni-channel)

(define ch (make-uni-channel (make-async-channel)))
(uni-channel-send ch 'hello)
(uni-channel-recv ch)

For port-channel, wrap the input and output endpoints separately:

(require port-channel
         uni-channel)

(define-values (in out) (make-pipe))
(define reader (make-uni-channel (make-port-channel in)))
(define writer (make-uni-channel (make-port-channel out)))

(uni-channel-send writer '(hello 1 2 3))
(uni-channel-recv reader)

For output port-channels, uni-channel-put queues the value for the writer thread. Use uni-channel-close followed by uni-channel-wait when shutdown must wait until queued values have actually been written and the writer thread has stopped.

Install and test:

raco pkg install --auto port-channel
raco pkg install --auto ./uni-channel
raco test -x -p uni-channel
raco setup --pkgs uni-channel