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.
127 lines
4.8 KiB
Rust
127 lines
4.8 KiB
Rust
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
|
// Licensed under the MIT License:
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
zap::generated_code!(pub mod addressbook_zap);
|
|
|
|
pub mod addressbook {
|
|
use crate::addressbook_zap::{address_book, person};
|
|
use zap::serialize_packed;
|
|
|
|
pub fn write_address_book() -> ::zap::Result<()> {
|
|
let mut message = ::zap::message::Builder::new_default();
|
|
|
|
let address_book = message.init_root::<address_book::Builder>();
|
|
let mut people = address_book.init_people(2);
|
|
|
|
let mut alice = people.reborrow().get(0);
|
|
alice.set_id(123);
|
|
alice.set_name("Alice");
|
|
alice.set_email("[email protected]");
|
|
|
|
let mut alice_phones = alice.reborrow().init_phones(1);
|
|
alice_phones.reborrow().get(0).set_number("555-1212");
|
|
alice_phones
|
|
.reborrow()
|
|
.get(0)
|
|
.set_type(person::phone_number::Type::Mobile);
|
|
|
|
alice.get_employment().set_school("MIT");
|
|
|
|
let mut bob = people.get(1);
|
|
bob.set_id(456);
|
|
bob.set_name("Bob");
|
|
bob.set_email("[email protected]");
|
|
|
|
let mut bob_phones = bob.reborrow().init_phones(2);
|
|
bob_phones.reborrow().get(0).set_number("555-4567");
|
|
bob_phones
|
|
.reborrow()
|
|
.get(0)
|
|
.set_type(person::phone_number::Type::Home);
|
|
bob_phones.reborrow().get(1).set_number("555-7654");
|
|
bob_phones
|
|
.reborrow()
|
|
.get(1)
|
|
.set_type(person::phone_number::Type::Work);
|
|
|
|
bob.get_employment().set_unemployed(());
|
|
|
|
serialize_packed::write_message(&mut ::std::io::stdout(), &message)
|
|
}
|
|
|
|
pub fn print_address_book() -> ::zap::Result<()> {
|
|
let stdin = ::std::io::stdin();
|
|
let message_reader = serialize_packed::read_message(
|
|
&mut stdin.lock(),
|
|
::zap::message::ReaderOptions::new(),
|
|
)?;
|
|
let address_book = message_reader.get_root::<address_book::Reader>()?;
|
|
|
|
for person in address_book.get_people()? {
|
|
println!(
|
|
"{}: {}",
|
|
person.get_name()?.to_str()?,
|
|
person.get_email()?.to_str()?
|
|
);
|
|
for phone in person.get_phones()? {
|
|
let type_name = match phone.get_type() {
|
|
Ok(person::phone_number::Type::Mobile) => "mobile",
|
|
Ok(person::phone_number::Type::Home) => "home",
|
|
Ok(person::phone_number::Type::Work) => "work",
|
|
Err(::zap::NotInSchema(_)) => "UNKNOWN",
|
|
};
|
|
println!(" {} phone: {}", type_name, phone.get_number()?.to_str()?);
|
|
}
|
|
match person.get_employment().which() {
|
|
Ok(person::employment::Unemployed(())) => {
|
|
println!(" unemployed");
|
|
}
|
|
Ok(person::employment::Employer(employer)) => {
|
|
println!(" employer: {}", employer?.to_str()?);
|
|
}
|
|
Ok(person::employment::School(school)) => {
|
|
println!(" student at: {}", school?.to_str()?);
|
|
}
|
|
Ok(person::employment::SelfEmployed(())) => {
|
|
println!(" self-employed");
|
|
}
|
|
Err(::zap::NotInSchema(_)) => {}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub fn main() {
|
|
let args: Vec<String> = ::std::env::args().collect();
|
|
if args.len() < 2 {
|
|
println!("usage: $ {} [write | read]", args[0]);
|
|
} else {
|
|
match &*args[1] {
|
|
"write" => addressbook::write_address_book().unwrap(),
|
|
"read" => addressbook::print_address_book().unwrap(),
|
|
_ => {
|
|
println!("unrecognized argument")
|
|
}
|
|
}
|
|
}
|
|
}
|