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