Skip to content

feat: add Connection::from_reader_writer for pre-connected transports - #148

Merged
haraldh merged 2 commits into
varlink:masterfrom
lsjostro:feat/connection-from-reader-writer
Jul 23, 2026
Merged

feat: add Connection::from_reader_writer for pre-connected transports#148
haraldh merged 2 commits into
varlink:masterfrom
lsjostro:feat/connection-from-reader-writer

Conversation

@lsjostro

@lsjostro lsjostro commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Add a public constructor that builds a Connection from an already-connected reader/writer pair, rather than resolving a varlink URI and opening the socket internally.

Motivation

Connection can currently only be constructed from a varlink address URI (with_address/with_activate/with_bridge) or by spawning a child. There is no supported way to drive the protocol over a transport the URI scheme does not cover — e.g. an AF_VSOCK socket, an anonymous pipe, or a pre-established socketpair.

Today the only options are to carry a downstream patch or build the Connection struct by literal (impossible externally, since the address field is private). We currently maintain exactly such a patch to talk to a VM guest over AF_VSOCK; this upstreams it so the use case is supported without forking the crate.

What this adds

pub fn from_reader_writer<R, W>(reader: R, writer: W) -> Arc<RwLock<Self>>
where
    R: Read + Send + Sync + 'static,
    W: Write + Send + Sync + 'static,

The caller owns connecting the transport; varlink only reads from reader and writes to writer. The resulting connection holds no address and no owned stream, so the underlying transport is not closed on drop — that stays the caller's responsibility. It then works with the generated VarlinkClient like any other Connection.

Testing

  • cargo build -p varlink — clean
  • cargo test -p varlink --doc — passes (incl. the new doctest)
  • cargo fmt -p varlink -- --check and cargo clippy -p varlink — clean

Add a public constructor that builds a Connection from an already-connected
reader/writer pair, instead of resolving a varlink URI and opening the socket
internally.

This covers transports the URI scheme does not, such as AF_VSOCK sockets,
anonymous pipes, and pre-established socketpairs: the caller connects the
transport and varlink only drives the protocol over the supplied handles. The
connection holds no address and no owned stream, so the transport is not closed
on drop.
@haraldh haraldh closed this Jul 23, 2026
@haraldh haraldh reopened this Jul 23, 2026
@haraldh

haraldh commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

@haraldh
haraldh merged commit 4f73812 into varlink:master Jul 23, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants