mirror of
https://github.com/zap-proto/rust.git
synced 2026-10-30 15:04:23 +00:00
Cap'n Proto was the canonical name for this Rust implementation. The
ZAP rebrand had only been applied as a directory rename so far —
crate names, schema file extensions, import paths, and binaries all
still said `capnp` / `capnpc`. This commit makes the rebrand real.
Workspace renames:
- crate capnp/ → zap/ (name = "zap")
- crate capnpc/ → zapc/ (name = "zapc")
- crate capnp-futures/ → zap-futures/
- crate capnp-rpc/ → zap-rpc/
Source-file renames:
- *_capnp.rs → *_zap.rs (~12 files: schema_zap.rs, rpc_zap.rs, etc.)
- capnpc-rust.rs → zapc-rust.rs (the codegen binary entry point)
- *.capnp → *.zap (33 schema files)
Identifier renames across the workspace (96 files rewritten):
- `use capnp::` → `use zap::`
- `extern crate capnp` → `extern crate zap`
- `capnp::generated_code!` → `zap::generated_code!`
- `capnp::traits::Owned` → `zap::traits::Owned`
- `capnp::message::...` → `zap::message::...`
- `capnp::Error/Result` → `zap::Error/Result`
- `capnp::Word` → `zap::Word`
- `capnp::primitive_list` → `zap::primitive_list`
- `capnp::any_pointer` → `zap::any_pointer`
- (etc. — see python script in commit history for full list)
Cargo.toml dep keys:
- `capnp = { path = ... }` → `zap = { path = ... }`
- `[dependencies.capnp]` → `[dependencies.zap]`
- `capnpc = { ... }` → `zapc = { ... }`
- Per-crate name= field and bin = "capnpc-rust" → "zapc"
Schema header strip:
- `using Cxx = import "/capnp/c++.capnp";` lines removed (C++ binding
hints; Rust output doesn't need them, and they referenced a system
/usr/include path we don't ship)
- `$Cxx.namespace("capnp::...")` annotations removed
Build-time decoupling for the benchmark:
- Pre-generated `carsales_zap.rs`, `catrank_zap.rs`, `eval_zap.rs` are
now committed in benchmark/. benchmark/build.rs just copies them
into OUT_DIR instead of invoking the schema compiler at build time.
- benchmark/Cargo.toml drops the [build-dependencies] zapc entry.
Building the benchmark needs ONLY the `zap` runtime crate; no
schema-compiler binary, no plugin, no shell-out.
To regenerate the schema-bound .rs from .zap (developer-only step):
cargo build --release --bin zapc
ln -sf zapc target/release/capnpc-rust # plugin discovery name
cd benchmark && PATH=../target/release:$PATH \
capnp compile -orust eval.zap catrank.zap carsales.zap
The `capnp` tool here is the upstream C++ schema-text parser — only
used as a parser frontend at codegen time, never linked into the
benchmark binary. Runtime + build = 100% zap/zapc only.
Verified by:
cargo tree -p benchmark --release → no capnp dep
grep -rE 'capnp|Capnp|CAPNP' . → 0 hits in active source
cargo build -p benchmark --release → clean build, no system tool
bash ./benchmark/bench.sh quick → runs end-to-end
Other workspace members (fill_random_values, addressbook,
wasm-hello-world, capnpc/test fixtures) still use build.rs schema
compilation at build time; they require a `zap` binary in PATH.
That's pre-existing — the upstream Cap'n Proto Rust workspace had the
same requirement against the `capnp` binary. Out of scope for this PR.
124 lines
3.6 KiB
Rust
124 lines
3.6 KiB
Rust
use std::{
|
|
cell::{Cell, RefCell},
|
|
net::ToSocketAddrs,
|
|
};
|
|
|
|
use crate::streaming_zap::{byte_stream, receiver};
|
|
use zap_rpc::{rpc_twoparty_zap, twoparty, RpcSystem};
|
|
|
|
use zap::Error;
|
|
|
|
use futures::channel::oneshot;
|
|
use futures::AsyncReadExt;
|
|
use sha2::{Digest, Sha256};
|
|
|
|
struct ByteStreamImpl {
|
|
hasher: RefCell<Sha256>,
|
|
bytes_received: Cell<u32>,
|
|
hash_sender: RefCell<Option<oneshot::Sender<Vec<u8>>>>,
|
|
}
|
|
|
|
impl ByteStreamImpl {
|
|
fn new(hash_sender: oneshot::Sender<Vec<u8>>) -> Self {
|
|
Self {
|
|
hasher: RefCell::new(Sha256::new()),
|
|
bytes_received: Cell::new(0),
|
|
hash_sender: RefCell::new(Some(hash_sender)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl byte_stream::Server for ByteStreamImpl {
|
|
async fn write(self: std::rc::Rc<Self>, params: byte_stream::WriteParams) -> Result<(), Error> {
|
|
let bytes = params.get()?.get_bytes()?;
|
|
self.hasher.borrow_mut().update(bytes);
|
|
self.bytes_received
|
|
.set(self.bytes_received.get() + bytes.len() as u32);
|
|
Ok(())
|
|
}
|
|
|
|
async fn end(
|
|
self: std::rc::Rc<Self>,
|
|
_params: byte_stream::EndParams,
|
|
_results: byte_stream::EndResults,
|
|
) -> Result<(), Error> {
|
|
let hasher = std::mem::take(&mut *self.hasher.borrow_mut());
|
|
let hash = hasher.finalize()[..].to_vec();
|
|
println!(
|
|
"received {} bytes with hash {}",
|
|
self.bytes_received.get(),
|
|
base16::encode_lower(&hash[..])
|
|
);
|
|
if let Some(sender) = self.hash_sender.borrow_mut().take() {
|
|
let _ = sender.send(hash);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
struct ReceiverImpl {}
|
|
|
|
impl ReceiverImpl {
|
|
fn new() -> Self {
|
|
Self {}
|
|
}
|
|
}
|
|
|
|
impl receiver::Server for ReceiverImpl {
|
|
async fn write_stream(
|
|
self: std::rc::Rc<Self>,
|
|
_params: receiver::WriteStreamParams,
|
|
mut results: receiver::WriteStreamResults,
|
|
) -> Result<(), Error> {
|
|
let (snd, rcv) = oneshot::channel();
|
|
let client: byte_stream::Client = zap_rpc::new_client(ByteStreamImpl::new(snd));
|
|
results.get().set_stream(client);
|
|
results.set_pipeline()?;
|
|
|
|
match rcv.await {
|
|
Ok(v) => {
|
|
results.get().set_sha256(&v[..]);
|
|
Ok(())
|
|
}
|
|
Err(_) => Err(Error::failed("failed to get hash".into())),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let args: Vec<String> = ::std::env::args().collect();
|
|
if args.len() != 3 {
|
|
println!("usage: {} server ADDRESS[:PORT]", args[0]);
|
|
return Ok(());
|
|
}
|
|
|
|
let addr = args[2]
|
|
.to_socket_addrs()?
|
|
.next()
|
|
.expect("could not parse address");
|
|
|
|
tokio::task::LocalSet::new()
|
|
.run_until(async move {
|
|
let listener = tokio::net::TcpListener::bind(&addr).await?;
|
|
let client: receiver::Client = zap_rpc::new_client(ReceiverImpl::new());
|
|
|
|
loop {
|
|
let (stream, _) = listener.accept().await?;
|
|
stream.set_nodelay(true)?;
|
|
let (reader, writer) =
|
|
tokio_util::compat::TokioAsyncReadCompatExt::compat(stream).split();
|
|
let network = twoparty::VatNetwork::new(
|
|
futures::io::BufReader::new(reader),
|
|
futures::io::BufWriter::new(writer),
|
|
rpc_twoparty_zap::Side::Server,
|
|
Default::default(),
|
|
);
|
|
|
|
let rpc_system = RpcSystem::new(Box::new(network), Some(client.clone().client));
|
|
|
|
tokio::task::spawn_local(rpc_system);
|
|
}
|
|
})
|
|
.await
|
|
}
|