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 all 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
217 changes: 137 additions & 80 deletions Cargo.lock

Large diffs are not rendered by default.

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
test = [ "rand" ]
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 }
3 changes: 1 addition & 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
5 changes: 4 additions & 1 deletion commons/zenoh-buffers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//

//! Provide differnt buffer implementations used for serialization and deserialization.
//! Provide different buffer implementations used for serialization and deserialization.
#![no_std]
extern crate alloc;

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
23 changes: 18 additions & 5 deletions commons/zenoh-codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,33 @@ 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-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
2 changes: 1 addition & 1 deletion 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
3 changes: 3 additions & 0 deletions commons/zenoh-codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;

mod common;
mod core;
mod scouting;
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/scouting/hello.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, Zenoh060Header};
use alloc::{vec, vec::Vec};
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/transport/frame.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, Zenoh060Header, Zenoh060Reliability};
use alloc::vec::Vec;
use zenoh_buffers::{
reader::{BacktrackableReader, DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/transport/join.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, Zenoh060Header};
use alloc::boxed::Box;
use core::time::Duration;
use zenoh_buffers::{
reader::{DidntRead, Reader},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/zenoh/declare.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, Zenoh060Header};
use alloc::vec::Vec;
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/zenoh/linkstate.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, Zenoh060Header};
use alloc::vec::Vec;
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
1 change: 1 addition & 0 deletions commons/zenoh-codec/src/zenoh/query.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, Zenoh060Header};
use alloc::string::String;
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
Expand Down
4 changes: 3 additions & 1 deletion commons/zenoh-collections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ description = "Internal crate for zenoh."
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
std = ["zenoh-core"]
default = ["std"]
std = ["zenoh-core"]
defmt = ["dep:defmt"]

[dependencies]
defmt = { version = "0.3.2", features = ["alloc"], optional = true }
zenoh-core = { version = "0.7.0-rc", path = "../zenoh-core/", optional = true }
3 changes: 3 additions & 0 deletions commons/zenoh-collections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
//! This crate is intended for Zenoh's internal use.
//!
//! [Click here for Zenoh's documentation](../zenoh/index.html)
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;

pub mod single_or_vec;
pub use single_or_vec::*;

Expand Down
5 changes: 4 additions & 1 deletion commons/zenoh-collections/src/single_or_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
use alloc::{vec, vec::Vec};
use core::{
cmp::PartialEq,
fmt, iter,
Expand All @@ -19,6 +20,7 @@ use core::{
};

#[derive(Clone, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
enum SingleOrVecInner<T> {
Single(T),
Vec(Vec<T>),
Expand Down Expand Up @@ -80,6 +82,7 @@ where
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SingleOrVec<T>(SingleOrVecInner<T>);

impl<T> SingleOrVec<T> {
Expand Down Expand Up @@ -197,7 +200,7 @@ impl<T> iter::Extend<T> for SingleOrVec<T> {
}

pub struct IntoIter<T> {
pub drain: std::vec::IntoIter<T>,
pub drain: alloc::vec::IntoIter<T>,
pub last: Option<T>,
}

Expand Down
33 changes: 24 additions & 9 deletions commons/zenoh-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,34 @@ categories = ["network-programming"]
description = "Internal crate for zenoh."

[features]
test = [ "rand", "zenoh-buffers/test" ]
shared-memory = []
default = ["std"]
std = [
"hex/std",
"rand?/std",
"rand?/std_rng",
"serde/std",
"uhlc/std",
"uuid/std"
]
test = ["rand", "zenoh-buffers/test"]
shared-memory = ["std"]
complete_n = []
defmt = [
"dep:defmt",
"uhlc/defmt",
"zenoh-buffers/defmt"
]

[dependencies]
hex = "0.4.3"
rand = { version = "0.8.5", optional = true }
serde = "1.0.147"
uhlc = "0.5.1"
uuid = "1.2.1"
zenoh-buffers = { version = "0.7.0-rc", path = "../zenoh-buffers/" }
defmt = { version = "0.3.2", features = ["alloc"], optional = true }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
rand = { version = "0.8.5", default-features = false, features = ["alloc", "getrandom"], optional = true }
serde = { version = "1.0.147", default-features = false, features = ["alloc"] }
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
uuid = { version = "1.2.1", default-features = false } # Needs a getrandom::getrandom() custom implementation on embedded (in root crate)
zenoh-buffers = { version = "0.7.0-rc", path = "../zenoh-buffers/", default-features = false }
zenoh-core = { version = "0.7.0-rc", path = "../zenoh-core/" }

[dev-dependencies]
lazy_static = "1.4.0"
zenoh-protocol = { version = "0.7.0-rc", path = ".", features = [ "test" ] }
zenoh-protocol = { version = "0.7.0-rc", path = ".", features = ["test"] }
Loading