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

res() -> wait() #1397

Merged
merged 2 commits into from
Sep 11, 2024
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
2 changes: 1 addition & 1 deletion commons/zenoh-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use zresult::{Error, ZResult as Result};

/// A resolvable execution, either sync or async
pub trait Resolvable {
type To: Sized + Send;
type To: Sized;
}

/// Trick used to mark `<Resolve as IntoFuture>::IntoFuture` bound as Send
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{
},
};

use zenoh_core::zlock;
use zenoh_core::{zlock, Resolvable, Wait};
use zenoh_result::ZResult;

use super::posix_shm_segment::PosixShmSegment;
Expand Down Expand Up @@ -108,10 +108,14 @@ pub struct LayoutedPosixShmProviderBackendBuilder<Layout: Borrow<MemoryLayout>>
layout: Layout,
}

impl<Layout: Borrow<MemoryLayout>> LayoutedPosixShmProviderBackendBuilder<Layout> {
/// try to create PosixShmProviderBackend
#[zenoh_macros::unstable_doc]
pub fn res(self) -> ZResult<PosixShmProviderBackend> {
#[zenoh_macros::unstable_doc]
impl<Layout: Borrow<MemoryLayout>> Resolvable for LayoutedPosixShmProviderBackendBuilder<Layout> {
type To = ZResult<PosixShmProviderBackend>;
}

#[zenoh_macros::unstable_doc]
impl<Layout: Borrow<MemoryLayout>> Wait for LayoutedPosixShmProviderBackendBuilder<Layout> {
fn wait(self) -> <Self as Resolvable>::To {
PosixShmProviderBackend::new(self.layout.borrow())
}
}
Expand Down
15 changes: 12 additions & 3 deletions commons/zenoh-shm/src/api/provider/shm_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,14 +719,23 @@ where
backend: Backend,
id: IDSource,
}
impl<IDSource, Backend> ShmProviderBuilderBackendID<IDSource, Backend>
#[zenoh_macros::unstable_doc]
impl<IDSource, Backend> Resolvable for ShmProviderBuilderBackendID<IDSource, Backend>
where
IDSource: ProtocolIDSource,
Backend: ShmProviderBackend,
{
type To = ShmProvider<IDSource, Backend>;
}

#[zenoh_macros::unstable_doc]
impl<IDSource, Backend> Wait for ShmProviderBuilderBackendID<IDSource, Backend>
where
IDSource: ProtocolIDSource,
Backend: ShmProviderBackend,
{
/// build ShmProvider
#[zenoh_macros::unstable_doc]
pub fn res(self) -> ShmProvider<IDSource, Backend> {
fn wait(self) -> <Self as Resolvable>::To {
ShmProvider::new(self.backend, self.id)
}
}
Expand Down
9 changes: 5 additions & 4 deletions commons/zenoh-shm/tests/posix_shm_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//

use zenoh_core::Wait;
use zenoh_shm::api::{
client::shm_client::ShmClient,
protocol_implementations::posix::{
Expand All @@ -31,7 +32,7 @@ fn posix_shm_provider_create() {
let _backend = PosixShmProviderBackend::builder()
.with_size(1024)
.expect("Error creating Layout!")
.res()
.wait()
.expect("Error creating PosixShmProviderBackend!");
}

Expand All @@ -40,7 +41,7 @@ fn posix_shm_provider_alloc() {
let backend = PosixShmProviderBackend::builder()
.with_size(1024)
.expect("Error creating Layout!")
.res()
.wait()
.expect("Error creating PosixShmProviderBackend!");

let layout = MemoryLayout::new(100, AllocAlignment::default()).unwrap();
Expand All @@ -55,7 +56,7 @@ fn posix_shm_provider_open() {
let backend = PosixShmProviderBackend::builder()
.with_size(1024)
.expect("Error creating Layout!")
.res()
.wait()
.expect("Error creating PosixShmProviderBackend!");

let layout = MemoryLayout::new(100, AllocAlignment::default()).unwrap();
Expand All @@ -76,7 +77,7 @@ fn posix_shm_provider_allocator() {
let backend = PosixShmProviderBackend::builder()
.with_size(BUFFER_SIZE * BUFFER_NUM)
.expect("Error creating Layout!")
.res()
.wait()
.expect("Error creating PosixShmProviderBackend!");

let layout = MemoryLayout::new(BUFFER_SIZE, AllocAlignment::default()).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions examples/examples/z_alloc_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use zenoh::{
AllocAlignment, BlockOn, Deallocate, Defragment, GarbageCollect, PosixShmProviderBackend,
ShmProviderBuilder, POSIX_PROTOCOL_ID,
},
Config,
Config, Wait,
};

#[tokio::main]
Expand All @@ -33,13 +33,13 @@ async fn run() -> ZResult<()> {
let backend = PosixShmProviderBackend::builder()
.with_size(65536)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// There are two API-defined ways of making shm buffer allocations: direct and through the layout...

Expand Down
4 changes: 2 additions & 2 deletions examples/examples/z_bytes_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ fn main() {
let backend = PosixShmProviderBackend::builder()
.with_size(4096)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// Allocate an SHM buffer
// NOTE: For allocation API please check z_alloc_shm.rs example
Expand Down
6 changes: 3 additions & 3 deletions examples/examples/z_get_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh::{
zshm, BlockOn, GarbageCollect, PosixShmProviderBackend, ShmProviderBuilder,
POSIX_PROTOCOL_ID,
},
Config,
Config, Wait,
};
use zenoh_examples::CommonArgs;

Expand All @@ -42,13 +42,13 @@ async fn main() {
let backend = PosixShmProviderBackend::builder()
.with_size(N * 1024)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// Allocate an SHM buffer
// NOTE: For allocation API please check z_alloc_shm.rs example
Expand Down
6 changes: 3 additions & 3 deletions examples/examples/z_ping_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh::{
prelude::*,
qos::CongestionControl,
shm::{PosixShmProviderBackend, ShmProviderBuilder, POSIX_PROTOCOL_ID},
Config,
Config, Wait,
};
use zenoh_examples::CommonArgs;

Expand Down Expand Up @@ -52,13 +52,13 @@ fn main() {
let backend = PosixShmProviderBackend::builder()
.with_size(size)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// Allocate an SHM buffer
// NOTE: For allocation API please check z_alloc_shm.rs example
Expand Down
12 changes: 8 additions & 4 deletions examples/examples/z_posix_shm_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
use zenoh::shm::{
AllocAlignment, MemoryLayout, PosixShmProviderBackend, ShmProviderBuilder, POSIX_PROTOCOL_ID,
use zenoh::{
shm::{
AllocAlignment, MemoryLayout, PosixShmProviderBackend, ShmProviderBuilder,
POSIX_PROTOCOL_ID,
},
Wait,
};

fn main() {
Expand All @@ -34,13 +38,13 @@ fn main() {
// Build a provider backend
PosixShmProviderBackend::builder()
.with_layout(provider_layout)
.res()
.wait()
.unwrap()
};

// Construct an SHM provider for particular backend and POSIX_PROTOCOL_ID
let _shm_provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();
}
6 changes: 3 additions & 3 deletions examples/examples/z_pub_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zenoh::{
shm::{
BlockOn, GarbageCollect, PosixShmProviderBackend, ShmProviderBuilder, POSIX_PROTOCOL_ID,
},
Config,
Config, Wait,
};
use zenoh_examples::CommonArgs;

Expand All @@ -40,13 +40,13 @@ async fn main() -> Result<(), ZError> {
let backend = PosixShmProviderBackend::builder()
.with_size(N * 1024)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

let publisher = session.declare_publisher(&path).await.unwrap();

Expand Down
6 changes: 3 additions & 3 deletions examples/examples/z_pub_shm_thr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use zenoh::{
prelude::*,
qos::CongestionControl,
shm::{PosixShmProviderBackend, ShmProviderBuilder, POSIX_PROTOCOL_ID},
Config,
Config, Wait,
};
use zenoh_examples::CommonArgs;

Expand All @@ -34,13 +34,13 @@ async fn main() {
let backend = PosixShmProviderBackend::builder()
.with_size(sm_size)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// Allocate an SHM buffer
// NOTE: For allocation API please check z_alloc_shm.rs example
Expand Down
6 changes: 3 additions & 3 deletions examples/examples/z_queryable_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh::{
zshm, BlockOn, GarbageCollect, PosixShmProviderBackend, ShmProviderBuilder,
POSIX_PROTOCOL_ID,
},
Config,
Config, Wait,
};
use zenoh_examples::CommonArgs;

Expand All @@ -42,13 +42,13 @@ async fn main() {
let backend = PosixShmProviderBackend::builder()
.with_size(N * 1024)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

println!("Declaring Queryable on '{key_expr}'...");
let queryable = session
Expand Down
6 changes: 3 additions & 3 deletions io/zenoh-transport/tests/unicast_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod tests {
};

use zenoh_buffers::buffer::SplitBuffer;
use zenoh_core::ztimeout;
use zenoh_core::{ztimeout, Wait};
use zenoh_link::Link;
use zenoh_protocol::{
core::{CongestionControl, Encoding, EndPoint, Priority, WhatAmI, ZenohIdProto},
Expand Down Expand Up @@ -161,12 +161,12 @@ mod tests {
let backend = PosixShmProviderBackend::builder()
.with_size(2 * MSG_SIZE)
.unwrap()
.res()
.wait()
.unwrap();
let shm01 = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// Create a peer manager with shared-memory authenticator enabled
let peer_shm01_handler = Arc::new(SHPeer::new(true));
Expand Down
8 changes: 4 additions & 4 deletions zenoh/src/api/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3263,8 +3263,6 @@ mod tests {

use rand::Rng;
use zenoh_buffers::{ZBuf, ZSlice};
#[cfg(feature = "shared-memory")]
use zenoh_core::Wait;
use zenoh_protocol::core::Parameters;
#[cfg(feature = "shared-memory")]
use zenoh_shm::api::{
Expand All @@ -3277,6 +3275,8 @@ mod tests {

use super::ZBytes;
use crate::bytes::{Deserialize, Serialize, ZSerde};
#[cfg(feature = "shared-memory")]
use crate::zenoh_core::Wait;

const NUM: usize = 1_000;

Expand Down Expand Up @@ -3439,13 +3439,13 @@ mod tests {
let backend = PosixShmProviderBackend::builder()
.with_size(4096)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// Prepare a layout for allocations
let layout = provider.alloc(1024).into_layout().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions zenoh/tests/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ fn shm_bytes_single_buf() {
let backend = PosixShmProviderBackend::builder()
.with_size(4096)
.unwrap()
.res()
.wait()
.unwrap();
// ...and an SHM provider
let provider = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// Prepare a layout for allocations
let layout = provider.alloc(1024).into_layout().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions zenoh/tests/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ async fn test_session_pubsub(peer01: &Session, peer02: &Session, reliability: Re
let backend = PosixShmProviderBackend::builder()
.with_size(size * MSG_COUNT / 10)
.unwrap()
.res()
.wait()
.unwrap();
// ...and SHM provider
let shm01 = ShmProviderBuilder::builder()
.protocol_id::<POSIX_PROTOCOL_ID>()
.backend(backend)
.res();
.wait();

// remember segment size that was allocated
let shm_segment_size = shm01.available();
Expand Down