Skip to content

Commit 5ccb542

Browse files
committed
Fix clippy errors
1 parent 337f4f9 commit 5ccb542

File tree

2 files changed

+92
-12
lines changed

2 files changed

+92
-12
lines changed

examples/nvs_get_set_c_style.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//! Updates tags from the NVS (default partition).
2+
//!
3+
//! Note that this module exposes two separate set of APIs:
4+
//! * the get_XXX/set_XXX API (where XXX is u8, str, etc.) - this is only for interop with C code that uses the C ESP IDF NVS API as well.
5+
//! * the `get_raw`/`set_raw` APIs that take a `&[u8]`. This is the "native" Rust API that implements the `RawStorage` trait from `embedded-svc` and it should be preferred actually, as you can layer on top of it any serde you want.
6+
//!
7+
//! More info reagarding NVS:
8+
//! https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/storage/nvs_flash.html
9+
10+
use esp_idf_sys::{self as _}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
11+
12+
use esp_idf_svc::log::EspLogger;
13+
use esp_idf_svc::nvs::*;
14+
use log::info;
15+
16+
fn main() -> anyhow::Result<()> {
17+
EspLogger::initialize_default();
18+
19+
let nvs_default_partition: EspNvsPartition<NvsDefault> =
20+
EspDefaultNvsPartition::take().unwrap();
21+
22+
let test_namespace = "test_ns";
23+
let mut nvs = match EspNvs::new(nvs_default_partition, test_namespace, true) {
24+
Ok(nvs) => {
25+
info!("Got namespace {:?} from default partition", test_namespace);
26+
nvs
27+
}
28+
Err(e) => panic!("Could't get namespace {:?}", e),
29+
};
30+
31+
let tag_u8 = "test_u8";
32+
33+
match nvs.set_u8(tag_u8, 42) {
34+
Ok(_) => info!("Tag updated"),
35+
// You can find the meaning of the error codes in the output of the error branch in:
36+
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/error-codes.html
37+
Err(e) => info!("Tag not updated {:?}", e),
38+
};
39+
40+
match nvs.get_u8(tag_u8).unwrap() {
41+
Some(v) => info!("{:?} = {:?}", tag_u8, v),
42+
None => info!("{:?} not found", tag_u8),
43+
};
44+
45+
let tag_test_str = "test_str";
46+
// String values are limited in the IDF to 4000 bytes, but our buffer is shorter.
47+
const MAX_STR_LEN: usize = 100;
48+
49+
let the_str_len: usize = nvs.str_len(tag_test_str).map_or(0, |v| {
50+
info!("Got stored string length of {:?}", v);
51+
let vv = v.unwrap_or(0);
52+
if vv >= MAX_STR_LEN {
53+
info!("Too long, trimming");
54+
0
55+
} else {
56+
vv
57+
}
58+
});
59+
60+
match the_str_len == 0 {
61+
true => info!("{:?} does not seem to exist", tag_test_str),
62+
false => {
63+
let mut buffer: [u8; MAX_STR_LEN] = [0; MAX_STR_LEN];
64+
match nvs.get_str(tag_test_str, &mut buffer).unwrap() {
65+
Some(v) => info!("{:?} = {:?}", tag_test_str, v),
66+
None => info!("We got nothing from {:?}", tag_test_str),
67+
};
68+
}
69+
};
70+
71+
match nvs.set_str(tag_test_str, "Hello from the NVS!") {
72+
Ok(_) => info!("{:?} updated", tag_test_str),
73+
Err(e) => info!("{:?} not updated {:?}", tag_test_str, e),
74+
};
75+
76+
Ok(())
77+
}

src/notify.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use core::cell::RefCell;
21
use core::ptr;
32
use core::time::Duration;
43

54
extern crate alloc;
5+
use alloc::boxed::Box;
66
use alloc::sync::{Arc, Weak};
77
use alloc::vec::Vec;
88

@@ -25,7 +25,7 @@ pub struct EspSubscriptionsRegistry {
2525
subscriptions: Mutex<
2626
Vec<(
2727
usize,
28-
Arc<RefCell<dyn for<'a> FnMut(&'a u32) + Send + 'static>>,
28+
Arc<Mutex<Box<dyn for<'a> FnMut(&'a u32) + Send + 'static>>>,
2929
)>,
3030
>,
3131
}
@@ -57,7 +57,7 @@ impl EspSubscriptionsRegistry {
5757

5858
self.subscriptions
5959
.lock()
60-
.push((subscription_id, Arc::new(RefCell::new(callback))));
60+
.push((subscription_id, Arc::new(Mutex::new(Box::new(callback)))));
6161

6262
Ok(subscription_id)
6363
}
@@ -87,7 +87,7 @@ impl EspSubscriptionsRegistry {
8787
.map(|(subscription_id, f)| (*subscription_id, f.clone()));
8888

8989
if let Some((subscription_id, f)) = next {
90-
f.borrow_mut()(&notification);
90+
f.lock()(&notification);
9191

9292
prev_id = Some(subscription_id);
9393
} else {
@@ -161,15 +161,18 @@ impl EspNotify {
161161
) != 0
162162
};
163163

164-
if created {
165-
Ok(Self {
166-
task: Arc::new(task),
167-
registry,
168-
})
169-
} else {
170-
unsafe { Weak::from_raw(registry_weak_ptr) };
164+
#[allow(clippy::all)]
165+
{
166+
if created {
167+
Ok(Self {
168+
task: Arc::new(task),
169+
registry,
170+
})
171+
} else {
172+
unsafe { Weak::from_raw(registry_weak_ptr) };
171173

172-
Err(EspError::from_infallible::<ESP_FAIL>())
174+
Err(EspError::from_infallible::<ESP_FAIL>())
175+
}
173176
}
174177
}
175178

0 commit comments

Comments
 (0)