Skip to main content

esp_nvs/
get.rs

1//! The `Get<T>` trait and its implementation in this module allows providing a single generic,
2//! overloaded function `get<T>()` for all supported types of the driver.
3
4use alloc::string::String;
5use alloc::vec::Vec;
6
7use crate::error::Error;
8use crate::platform::Platform;
9use crate::{
10    Key,
11    Nvs,
12    raw,
13};
14
15pub trait Get<T> {
16    fn get(&mut self, namespace: &Key, key: &Key) -> Result<T, Error>;
17}
18
19impl<T, G: Get<T>> Get<T> for &mut G {
20    fn get(&mut self, namespace: &Key, key: &Key) -> Result<T, Error> {
21        (*self).get(namespace, key)
22    }
23}
24
25impl<T: Platform> Get<bool> for Nvs<T> {
26    fn get(&mut self, namespace: &Key, key: &Key) -> Result<bool, Error> {
27        let value = self.get_primitive(namespace, key, raw::ItemType::U8)?;
28        Ok(value as u8 != 0)
29    }
30}
31
32impl<T: Platform> Get<u8> for Nvs<T> {
33    fn get(&mut self, namespace: &Key, key: &Key) -> Result<u8, Error> {
34        let value = self.get_primitive(namespace, key, raw::ItemType::U8)?;
35        Ok(value as u8)
36    }
37}
38
39impl<T: Platform> Get<u16> for Nvs<T> {
40    fn get(&mut self, namespace: &Key, key: &Key) -> Result<u16, Error> {
41        let value = self.get_primitive(namespace, key, raw::ItemType::U16)?;
42        Ok(value as u16)
43    }
44}
45
46impl<T: Platform> Get<u32> for Nvs<T> {
47    fn get(&mut self, namespace: &Key, key: &Key) -> Result<u32, Error> {
48        let value = self.get_primitive(namespace, key, raw::ItemType::U32)?;
49        Ok(value as u32)
50    }
51}
52
53impl<T: Platform> Get<u64> for Nvs<T> {
54    fn get(&mut self, namespace: &Key, key: &Key) -> Result<u64, Error> {
55        let value = self.get_primitive(namespace, key, raw::ItemType::U64)?;
56        Ok(value)
57    }
58}
59
60impl<T: Platform> Get<i8> for Nvs<T> {
61    fn get(&mut self, namespace: &Key, key: &Key) -> Result<i8, Error> {
62        let value = self.get_primitive(namespace, key, raw::ItemType::I8)?;
63        Ok(value.cast_signed() as i8)
64    }
65}
66
67impl<T: Platform> Get<i16> for Nvs<T> {
68    fn get(&mut self, namespace: &Key, key: &Key) -> Result<i16, Error> {
69        let value = self.get_primitive(namespace, key, raw::ItemType::I16)?;
70        Ok(value.cast_signed() as i16)
71    }
72}
73
74impl<T: Platform> Get<i32> for Nvs<T> {
75    fn get(&mut self, namespace: &Key, key: &Key) -> Result<i32, Error> {
76        let value = self.get_primitive(namespace, key, raw::ItemType::I32)?;
77        Ok(value.cast_signed() as i32)
78    }
79}
80
81impl<T: Platform> Get<i64> for Nvs<T> {
82    fn get(&mut self, namespace: &Key, key: &Key) -> Result<i64, Error> {
83        let value = self.get_primitive(namespace, key, raw::ItemType::I64)?;
84        Ok(value.cast_signed())
85    }
86}
87
88impl<T: Platform> Get<String> for Nvs<T> {
89    fn get(&mut self, namespace: &Key, key: &Key) -> Result<String, Error> {
90        self.get_string(namespace, key)
91    }
92}
93
94impl<T: Platform> Get<Vec<u8>> for Nvs<T> {
95    fn get(&mut self, namespace: &Key, key: &Key) -> Result<Vec<u8>, Error> {
96        self.get_blob(namespace, key)
97    }
98}