mirror of
https://github.com/zap-proto/rust.git
synced 2026-10-30 15:04:23 +00:00
Two pre-existing CI failures (unrelated to codegen/install-zap): - minrust 1.81: quickcheck 1.1.0 pulls rand 0.10 -> rand_core 0.10.1, whose Cargo.toml requires the edition2024 feature (unstable on Cargo 1.81). Pin quickcheck to =1.0.3 (last version on rand 0.8 / rand_core 0.6.4) — within MSRV, no major bump. Fresh-resolve verified: rand_core 0.10.1 leaves the graph. - formatting: ran 'cargo +nightly fmt --all'; the CI check (--check --unstable-features --error-on-unformatted, nightly) now passes.
100 lines
3.8 KiB
Rust
100 lines
3.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);
|
|
|
|
use std::sync::mpsc;
|
|
use std::thread;
|
|
use zap::message::{Builder, HeapAllocator, TypedReader};
|
|
|
|
pub mod addressbook {
|
|
use crate::addressbook_zap::{address_book, person};
|
|
use zap::message::{Builder, HeapAllocator, TypedBuilder, TypedReader};
|
|
|
|
pub fn build_address_book() -> TypedReader<Builder<HeapAllocator>, address_book::Owned> {
|
|
let mut message = TypedBuilder::<address_book::Owned>::new_default();
|
|
let address_book = message.init_root();
|
|
|
|
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(());
|
|
|
|
// There are a few ways to get a TypedReader from our `message`:
|
|
//
|
|
// Option 1: Go from TypedBuilder into TypedReader directly by calling helper method
|
|
// message.into_reader()
|
|
//
|
|
// Option 2: Use the "Into" trait defined on the TypedBuilder
|
|
// message.into()
|
|
//
|
|
// Option 3: Use the "From" trait defined on the TypedReader
|
|
TypedReader::from(message)
|
|
}
|
|
}
|
|
|
|
pub fn main() {
|
|
let book = addressbook::build_address_book();
|
|
|
|
let (tx_book, rx_book) = mpsc::channel::<
|
|
TypedReader<Builder<HeapAllocator>, addressbook_zap::address_book::Owned>,
|
|
>();
|
|
let (tx_id, rx_id) = mpsc::channel::<u32>();
|
|
|
|
thread::spawn(move || {
|
|
let addressbook_reader = rx_book.recv().unwrap();
|
|
let addressbook = addressbook_reader.get().unwrap();
|
|
let first_person = addressbook.get_people().unwrap().get(0);
|
|
let first_id = first_person.get_id();
|
|
tx_id.send(first_id)
|
|
});
|
|
|
|
tx_book.send(book).unwrap();
|
|
let first_id = rx_id.recv().unwrap();
|
|
assert_eq!(first_id, 123);
|
|
}
|