Skip to content

Commit 287d5ac

Browse files
authored
Polish shm (#2023)
* make `with_size` method for POSIX backend builder infallable * add `as_mut_unchecked` method to all SHM buffer types * add alignment construction methods `for_type` and `for_val` * add alignment setting for default SHM Provider builder * code format for new rust
1 parent ffbe455 commit 287d5ac

File tree

65 files changed

+287
-245
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+287
-245
lines changed

commons/zenoh-codec/tests/codec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn zbuf_test() {
4141

4242
let codec = Zenoh080::new();
4343
codec.write(&mut writer, &zbuf).unwrap();
44-
println!("Buffer: {:?}", buffer);
44+
println!("Buffer: {buffer:?}");
4545

4646
let mut reader = buffer.reader();
4747
let ret: ZBuf = codec.read(&mut reader).unwrap();
@@ -178,7 +178,7 @@ fn codec_zint_len() {
178178
let mut writer = buff.writer();
179179
let n: u64 = 1 << (7 * i);
180180
codec.write(&mut writer, n).unwrap();
181-
println!("ZInt len: {} {:02x?}", n, buff);
181+
println!("ZInt len: {n} {buff:02x?}");
182182
assert_eq!(codec.w_len(n), buff.len());
183183
}
184184

commons/zenoh-config/src/include.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ where
136136

137137
values.remove(k);
138138
if let Some(include_values) = v.as_object_mut() {
139-
let title = format!(
140-
"{}.{} -> {}::{}",
141-
title, include_property_name, include_path, k
142-
);
139+
let title = format!("{title}.{include_property_name} -> {include_path}::{k}");
143140
recursive_include(
144141
title.as_str(),
145142
include_values,
@@ -157,7 +154,7 @@ where
157154
// process remaining object values
158155
for (k, v) in values.iter_mut() {
159156
if let Some(object) = v.as_object_mut() {
160-
let title = format!("{}.{}", title, k);
157+
let title = format!("{title}.{k}");
161158
recursive_include(
162159
title.as_str(),
163160
object,

commons/zenoh-config/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub enum InterceptorLink {
242242

243243
impl std::fmt::Display for InterceptorLink {
244244
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
245-
write!(f, "Transport({:?})", self)
245+
write!(f, "Transport({self:?})")
246246
}
247247
}
248248

@@ -1162,7 +1162,7 @@ impl PluginsConfig {
11621162
let required = match value.get("__required__") {
11631163
None => false,
11641164
Some(Value::Bool(b)) => *b,
1165-
_ => panic!("Plugin '{}' has an invalid '__required__' configuration property (must be a boolean)", id)
1165+
_ => panic!("Plugin '{id}' has an invalid '__required__' configuration property (must be a boolean)")
11661166
};
11671167
let name = match value.get("__plugin__") {
11681168
Some(Value::String(p)) => p,
@@ -1172,8 +1172,8 @@ impl PluginsConfig {
11721172
if let Some(paths) = value.get("__path__") {
11731173
let paths = match paths {
11741174
Value::String(s) => vec![s.clone()],
1175-
Value::Array(a) => a.iter().map(|s| if let Value::String(s) = s { s.clone() } else { panic!("Plugin '{}' has an invalid '__path__' configuration property (must be either string or array of strings)", id) }).collect(),
1176-
_ => panic!("Plugin '{}' has an invalid '__path__' configuration property (must be either string or array of strings)", id)
1175+
Value::Array(a) => a.iter().map(|s| if let Value::String(s) = s { s.clone() } else { panic!("Plugin '{id}' has an invalid '__path__' configuration property (must be either string or array of strings)") }).collect(),
1176+
_ => panic!("Plugin '{id}' has an invalid '__path__' configuration property (must be either string or array of strings)")
11771177
};
11781178
PluginLoad { id: id.clone(), name: name.clone(), paths: Some(paths), required }
11791179
} else {
@@ -1286,7 +1286,7 @@ impl std::fmt::Debug for PluginsConfig {
12861286
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12871287
let mut values: Value = self.values.clone();
12881288
sift_privates(&mut values);
1289-
write!(f, "{:?}", values)
1289+
write!(f, "{values:?}")
12901290
}
12911291
}
12921292

commons/zenoh-protocol/src/core/endpoint.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl fmt::Display for Protocol<'_> {
7878

7979
impl fmt::Debug for Protocol<'_> {
8080
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81-
write!(f, "{}", self)
81+
write!(f, "{self}")
8282
}
8383
}
8484

@@ -113,7 +113,7 @@ impl fmt::Display for ProtocolMut<'_> {
113113

114114
impl fmt::Debug for ProtocolMut<'_> {
115115
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116-
write!(f, "{}", self)
116+
write!(f, "{self}")
117117
}
118118
}
119119

@@ -142,7 +142,7 @@ impl fmt::Display for Address<'_> {
142142

143143
impl fmt::Debug for Address<'_> {
144144
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145-
write!(f, "{}", self)
145+
write!(f, "{self}")
146146
}
147147
}
148148

@@ -183,7 +183,7 @@ impl fmt::Display for AddressMut<'_> {
183183

184184
impl fmt::Debug for AddressMut<'_> {
185185
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186-
write!(f, "{}", self)
186+
write!(f, "{self}")
187187
}
188188
}
189189

@@ -231,7 +231,7 @@ impl fmt::Display for Metadata<'_> {
231231

232232
impl fmt::Debug for Metadata<'_> {
233233
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234-
write!(f, "{}", self)
234+
write!(f, "{self}")
235235
}
236236
}
237237

@@ -316,7 +316,7 @@ impl fmt::Display for MetadataMut<'_> {
316316

317317
impl fmt::Debug for MetadataMut<'_> {
318318
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
319-
write!(f, "{}", self)
319+
write!(f, "{self}")
320320
}
321321
}
322322

@@ -361,7 +361,7 @@ impl fmt::Display for Config<'_> {
361361

362362
impl fmt::Debug for Config<'_> {
363363
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
364-
write!(f, "{}", self)
364+
write!(f, "{self}")
365365
}
366366
}
367367

@@ -446,7 +446,7 @@ impl fmt::Display for ConfigMut<'_> {
446446

447447
impl fmt::Debug for ConfigMut<'_> {
448448
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
449-
write!(f, "{}", self)
449+
write!(f, "{self}")
450450
}
451451
}
452452

@@ -577,7 +577,7 @@ impl fmt::Display for EndPoint {
577577

578578
impl fmt::Debug for EndPoint {
579579
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
580-
write!(f, "{}", self)
580+
write!(f, "{self}")
581581
}
582582
}
583583

commons/zenoh-protocol/src/core/locator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl fmt::Display for Locator {
118118

119119
impl fmt::Debug for Locator {
120120
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121-
write!(f, "{}", self)
121+
write!(f, "{self}")
122122
}
123123
}
124124

commons/zenoh-protocol/src/core/parameters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ impl fmt::Display for Parameters<'_> {
466466

467467
impl fmt::Debug for Parameters<'_> {
468468
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
469-
write!(f, "{}", self)
469+
write!(f, "{self}")
470470
}
471471
}
472472

commons/zenoh-runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl RuntimeParam {
7676
.get(&zrt)
7777
.unwrap()
7878
.fetch_add(1, Ordering::SeqCst);
79-
format!("{}-{}", zrt, id)
79+
format!("{zrt}-{id}")
8080
})
8181
.build()?;
8282
Ok(rt)

commons/zenoh-shm/src/api/buffer/traits.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ pub enum BufferRelayoutError {
3131
pub trait ShmBuf: Deref<Target = [u8]> + AsRef<[u8]> {
3232
#[zenoh_macros::unstable_doc]
3333
fn is_valid(&self) -> bool;
34+
35+
#[zenoh_macros::unstable_doc]
36+
/// Get unchecked mutable access to buffer's memory. This is unsafe yet very powerful API for
37+
/// building concurrent access logic around SHM buffer contents. For safe version please
38+
/// see `ShmBufMut` trait
39+
///
40+
/// # Safety
41+
///
42+
/// Safe if multiple conditions are met:
43+
/// - user code guarantees no data race across all applications that share the buffer
44+
/// - the buffer is not being concurrently sent to the outside of SHM domain
45+
/// - the buffer is valid
46+
unsafe fn as_mut_unchecked(&mut self) -> &mut [u8];
3447
}
3548

3649
#[zenoh_macros::unstable_doc]

commons/zenoh-shm/src/api/buffer/zshm.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ impl ShmBuf for ZShm {
3636
fn is_valid(&self) -> bool {
3737
self.0.is_valid()
3838
}
39+
40+
unsafe fn as_mut_unchecked(&mut self) -> &mut [u8] {
41+
self.0.as_mut_slice_inner()
42+
}
3943
}
4044

4145
impl OwnedShmBuf for ZShm {
@@ -137,6 +141,10 @@ impl ShmBuf for zshm {
137141
fn is_valid(&self) -> bool {
138142
self.0.is_valid()
139143
}
144+
145+
unsafe fn as_mut_unchecked(&mut self) -> &mut [u8] {
146+
self.0.as_mut_slice_inner()
147+
}
140148
}
141149

142150
impl Deref for zshm {

commons/zenoh-shm/src/api/buffer/zshmmut.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ impl ShmBuf for ZShmMut {
3636
fn is_valid(&self) -> bool {
3737
self.0.is_valid()
3838
}
39+
40+
unsafe fn as_mut_unchecked(&mut self) -> &mut [u8] {
41+
self.0.as_mut_slice_inner()
42+
}
3943
}
4044

4145
impl ShmBufMut for ZShmMut {}
@@ -153,6 +157,10 @@ impl ShmBuf for zshmmut {
153157
fn is_valid(&self) -> bool {
154158
self.0.is_valid()
155159
}
160+
161+
unsafe fn as_mut_unchecked(&mut self) -> &mut [u8] {
162+
self.0.as_mut_slice_inner()
163+
}
156164
}
157165

158166
impl ShmBufMut for zshmmut {}

0 commit comments

Comments
 (0)