Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_std support #419

Merged
merged 16 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 73 additions & 3 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion commons/zenoh-buffers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ description = "Internal crate for zenoh."

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
test = [ "rand" ]
default = ["std"]
std = ["zenoh-collections/std"]
davidedellagiustina marked this conversation as resolved.
Show resolved Hide resolved
test = ["rand"]
defmt = ["dep:defmt", "zenoh-collections/defmt"]

[dependencies]
defmt = { version = "0.3.2", features = ["alloc"], optional = true }
rand = { version = "0.8.5", optional = true }
zenoh-collections = { version = "0.7.0-rc", path = "../zenoh-collections/", default-features = false }
4 changes: 2 additions & 2 deletions commons/zenoh-buffers/src/bbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
extern crate alloc;

use crate::{
reader::HasReader,
vec,
Expand All @@ -22,6 +20,7 @@ use alloc::boxed::Box;
use core::num::NonZeroUsize;

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct BBuf {
buffer: Box<[u8]>,
len: usize,
Expand Down Expand Up @@ -131,6 +130,7 @@ impl<'a> HasReader for &'a BBuf {
#[cfg(feature = "test")]
impl BBuf {
pub fn rand(len: usize) -> Self {
#[allow(unused_imports)]
davidedellagiustina marked this conversation as resolved.
Show resolved Hide resolved
use alloc::vec::Vec;
use rand::Rng;

Expand Down
5 changes: 4 additions & 1 deletion commons/zenoh-buffers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//

//! Provide differnt buffer implementations used for serialization and deserialization.
#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]
davidedellagiustina marked this conversation as resolved.
Show resolved Hide resolved
extern crate alloc;

mod bbuf;
Expand All @@ -32,6 +32,7 @@ pub mod writer {
use core::num::NonZeroUsize;

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DidntWrite;

pub trait Writer {
Expand Down Expand Up @@ -74,6 +75,7 @@ pub mod reader {
use core::num::NonZeroUsize;

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DidntRead;

pub trait Reader {
Expand Down Expand Up @@ -114,6 +116,7 @@ pub mod reader {
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DidntSiphon;

pub trait SiphonableReader: Reader {
Expand Down
2 changes: 0 additions & 2 deletions commons/zenoh-buffers/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
extern crate alloc;

use crate::{
reader::HasReader,
writer::{BacktrackableWriter, DidntWrite, HasWriter, Writer},
Expand Down
6 changes: 4 additions & 2 deletions commons/zenoh-buffers/src/zbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
extern crate alloc;

use crate::{
reader::{BacktrackableReader, DidntRead, DidntSiphon, HasReader, Reader, SiphonableReader},
writer::{BacktrackableWriter, DidntWrite, HasWriter, Writer},
Expand All @@ -27,6 +25,7 @@ fn get_mut_unchecked<T>(arc: &mut Arc<T>) -> &mut T {
}

#[derive(Debug, Clone, Default, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ZBuf {
slices: SingleOrVec<ZSlice>,
}
Expand Down Expand Up @@ -125,12 +124,14 @@ where

// Reader
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ZBufPos {
slice: usize,
byte: usize,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ZBufReader<'a> {
inner: &'a ZBuf,
cursor: ZBufPos,
Expand Down Expand Up @@ -348,6 +349,7 @@ impl Iterator for ZBufSliceIterator<'_, '_> {

// Writer
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ZBufWriter<'a> {
inner: &'a mut ZBuf,
cache: Arc<Vec<u8>>,
Expand Down
9 changes: 7 additions & 2 deletions commons/zenoh-buffers/src/zslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
extern crate alloc;

use crate::reader::{BacktrackableReader, DidntRead, HasReader, Reader};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use core::{
Expand Down Expand Up @@ -213,6 +211,13 @@ impl fmt::Debug for ZSlice {
}
}

#[cfg(feature = "defmt")]
impl defmt::Format for ZSlice {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{:02x}", self.as_slice());
}
}

// From impls
impl<T> From<Arc<T>> for ZSlice
where
Expand Down
24 changes: 19 additions & 5 deletions commons/zenoh-codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,34 @@ description = "Internal crate for zenoh."
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
shared-memory = ["zenoh-shm", "zenoh-protocol/shared-memory"]
default = ["std"]
std = [
"uhlc/std",
"zenoh-buffers/std",
davidedellagiustina marked this conversation as resolved.
Show resolved Hide resolved
"zenoh-protocol/std"
]
shared-memory = [
"std",
"zenoh-shm",
"zenoh-protocol/shared-memory"
]
complete_n = ["zenoh-protocol/complete_n"]
defmt = [
"zenoh-buffers/defmt",
"zenoh-protocol/defmt"
]

[dependencies]
uhlc = "0.5.1"
zenoh-buffers = { version = "0.7.0-rc", path = "../zenoh-buffers/" }
zenoh-protocol = { version = "0.7.0-rc", path = "../zenoh-protocol/" }
uhlc = { git = "https://github.com/atolab/uhlc-rs.git", default-features = false } # TODO: Using github source until the no_std update gets released on crates.io
zenoh-buffers = { version = "0.7.0-rc", path = "../zenoh-buffers/", default-features = false }
zenoh-protocol = { version = "0.7.0-rc", path = "../zenoh-protocol/", default-features = false }
zenoh-shm = { version = "0.7.0-rc", path = "../zenoh-shm/", optional = true }

[dev-dependencies]
criterion = "0.4.0"
rand = "0.8.5"
uuid = { version = "1.1.2", features = ["v4"] }
zenoh-protocol = { version = "0.7.0-rc", path = "../zenoh-protocol/", features = [ "test" ] }
zenoh-protocol = { version = "0.7.0-rc", path = "../zenoh-protocol/", features = ["test"] }

[[bench]]
name = "codec"
Expand Down
2 changes: 1 addition & 1 deletion commons/zenoh-codec/src/common/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use zenoh_protocol::{
transport::tmsg,
};
#[cfg(feature = "shared-memory")]
use {crate::Zenoh060Condition, std::any::TypeId, zenoh_shm::SharedMemoryBufInfoSerialized};
use {crate::Zenoh060Condition, core::any::TypeId, zenoh_shm::SharedMemoryBufInfoSerialized};

impl<W> WCodec<&Attachment, &mut W> for Zenoh060
where
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/core/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//
use crate::{RCodec, WCodec, Zenoh060};
use alloc::string::String;
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/core/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//
use crate::{RCodec, WCodec, Zenoh060};
use alloc::{string::String, vec::Vec};
use core::convert::TryFrom;
use zenoh_buffers::{
reader::{DidntRead, Reader},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/core/keyexpr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//
use crate::{RCodec, WCodec, Zenoh060, Zenoh060Condition};
use alloc::string::String;
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/core/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//
use crate::{RCodec, WCodec, Zenoh060};
use alloc::{string::String, vec::Vec};
use core::convert::TryFrom;
use zenoh_buffers::{
reader::{DidntRead, Reader},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod zint;
mod zslice;

use crate::{RCodec, WCodec, Zenoh060};
use alloc::{string::String, vec::Vec};
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/core/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//
use crate::{RCodec, WCodec, Zenoh060};
use alloc::vec::Vec;
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
4 changes: 2 additions & 2 deletions commons/zenoh-codec/src/core/zbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_buffers::{
};
#[cfg(feature = "shared-memory")]
use {
crate::Zenoh060Condition, std::any::TypeId, zenoh_buffers::ZSlice,
crate::Zenoh060Condition, core::any::TypeId, zenoh_buffers::ZSlice,
zenoh_shm::SharedMemoryBufInfoSerialized,
};

Expand Down Expand Up @@ -103,7 +103,7 @@ where
reader.read_zslices(len, |s| zbuf.push_zslice(s))?;
}
super::zslice::kind::SHM_INFO => {
let bytes: Vec<u8> = self.codec.read(&mut *reader)?;
let bytes: alloc::vec::Vec<u8> = self.codec.read(&mut *reader)?;
davidedellagiustina marked this conversation as resolved.
Show resolved Hide resolved
let shm_info: SharedMemoryBufInfoSerialized = bytes.into();
let zslice: ZSlice = shm_info.into();
zbuf.push_zslice(zslice);
Expand Down
Loading