Skip to content

Commit

Permalink
feat!: move serialization into zenoh-ext (#1473)
Browse files Browse the repository at this point in the history
* feat!: move serialization into zenoh-ext

* refactor: rename write_xxx/read_xxx to (de)serialize_xxx

* fix: lints

* fix: fix `ZBytes::as_shm`

* fix: make protoc non-mandatory to execute z_bytes example

* fix: lint

* fix: lint

* fix: lint

* fix: add Serialize implementation for ZBytes

* fix: lints

* refactor: replace extension traits by dedicated types

* fix: lints

* fix: add `?Sized` bound to `z_serialize`

* fix: add VarInt implementation

* feat!: ZBytesWriter/ZSerializer have no more lifetime

* feat: use bytemuck for `VarInt` slice conversion

* feat: remove bytemuck depedency

* fix: remove litigious `From` implementation

* fix: fix tests

* fix: fix tests

* docs: add varint example

* fix: typo

* fix: fix serialization test

* fix: outdated doc

* feat: add `ZDeserializer::deserialize_n`

* fix: typo

* fix: remove useless dependency

* fix: fix test

* serialization for zenoh types Encoding and Timestamp

* fix: test

* feat: implement serialization for array as variable-size collection

* docs: add array example in z_bytes

* code formatting

---------

Co-authored-by: Michael Ilyin <[email protected]>
  • Loading branch information
wyfo and milyin authored Oct 1, 2024
1 parent 09d1d1d commit deb411a
Show file tree
Hide file tree
Showing 50 changed files with 1,340 additions and 3,875 deletions.
98 changes: 81 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ json5 = "0.4.1"
jsonschema = { version = "0.20", default-features = false }
keyed-set = "1.0.0"
lazy_static = "1.5.0"
leb128 = "0.2"
libc = "0.2.158"
libloading = "0.8"
tracing = "0.1"
Expand Down Expand Up @@ -176,7 +177,6 @@ tokio-rustls = { version = "0.26.0", default-features = false }
thread-priority = "1.1.0"
typenum = "1.17.0"
uhlc = { version = "0.8.0", default-features = false } # Default features are disabled due to usage in no_std crates
unwrap-infallible = "0.1.5"
unzip-n = "0.1.2"
url = "2.5.2"
urlencoding = "2.1.3"
Expand Down
4 changes: 2 additions & 2 deletions ci/valgrind-check/src/pub_sub/bin/z_pub_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ async fn main() {
sample.key_expr().as_str(),
sample
.payload()
.deserialize::<String>()
.unwrap_or_else(|e| format!("{}", e))
.try_to_string()
.unwrap_or_else(|e| e.to_string().into())
);
})
.await
Expand Down
10 changes: 5 additions & 5 deletions ci/valgrind-check/src/queryable_get/bin/z_queryable_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn main() {
println!("Sending Query '{get_selector}'...");
let replies = get_session
.get(&get_selector)
.payload(idx)
.payload(idx.to_string())
.target(QueryTarget::All)
.await
.unwrap();
Expand All @@ -64,14 +64,14 @@ async fn main() {
sample.key_expr().as_str(),
sample
.payload()
.deserialize::<String>()
.unwrap_or_else(|e| format!("{}", e))
.try_to_string()
.unwrap_or_else(|e| e.to_string().into())
),
Err(err) => println!(
">> Received (ERROR: '{}')",
err.payload()
.deserialize::<String>()
.unwrap_or_else(|e| format!("{}", e))
.try_to_string()
.unwrap_or_else(|e| e.to_string().into())
),
}
}
Expand Down
6 changes: 5 additions & 1 deletion commons/zenoh-buffers/src/zbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ impl ZBuf {
self.slices.as_mut().iter_mut()
}

pub fn into_zslices(self) -> impl Iterator<Item = ZSlice> {
self.slices.into_iter()
}

pub fn push_zslice(&mut self, zslice: ZSlice) {
if !zslice.is_empty() {
self.slices.push(zslice);
Expand Down Expand Up @@ -404,7 +408,7 @@ impl<'a> io::Seek for ZBufReader<'a> {
.fold(0, |acc, s| acc + s.len())
+ self.cursor.byte;
let current_pos = i64::try_from(current_pos)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)))?;
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

let offset = match pos {
std::io::SeekFrom::Start(s) => i64::try_from(s).unwrap_or(i64::MAX) - current_pos,
Expand Down
5 changes: 2 additions & 3 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ clap = { workspace = true, features = ["derive"] }
futures = { workspace = true }
prost = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread", "time", "io-std"] }
unwrap-infallible = { workspace = true }
zenoh = { workspace = true, default-features = true }
zenoh-ext = { workspace = true }

[dev-dependencies]
rand = { workspace = true, features = ["default"] }

[build-dependencies]
rustc_version = { workspace = true }
prost-build = "0.13"
which = "6"

[[example]]
name = "z_scout"
Expand Down
16 changes: 16 additions & 0 deletions examples/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::{env, fs::File, io::Write, path::Path};

use which::which;

fn main() -> std::io::Result<()> {
// If protoc is not installed, we cheat because building protoc from source
// with protobuf-src is way too long
if which("protoc").is_err() {
const PROTO: &str = r#"#[derive(Clone, PartialEq, ::prost::Message)] pub struct Entity { #[prost(uint32, tag = "1")] pub id: u32, #[prost(string, tag = "2")] pub name: ::prost::alloc::string::String,}"#;
let out_path = Path::new(&env::var("OUT_DIR").unwrap()).join("example.rs");
File::create(out_path)?.write_all(PROTO.as_bytes())?;
return Ok(());
}
prost_build::compile_protos(&["examples/example.proto"], &["examples/"])?;
Ok(())
}
Loading

0 comments on commit deb411a

Please sign in to comment.