1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use super::ffi;


#[const_trait]
pub trait Record {
	const LEN: usize;

	fn tag(&self) -> &[u8; Self::LEN];
}

#[const_trait]
pub trait RecordTagRef {
	fn tag_ref(&self) -> &[u8];
}
impl<T: ~const Record> const RecordTagRef for T where [(); Self::LEN]: Sized {
	fn tag_ref(&self) -> &[u8] { self.tag().as_slice() }
}


pub trait RecordExt: Record {
	fn exists(&self) -> bool;
}

impl<T: Record + RecordTagRef> RecordExt for T {
	default fn exists(&self) -> bool { unsafe { ffi::furi_record_exists(self.tag_ref().as_ptr() as _) } }
}

// TODO: Enable when full specialization stabilized.
#[cfg(feature = "specialization")]
impl<T: Record> RecordExt for T where [(); T::LEN]: Sized {
	fn exists(&self) -> bool { unsafe { ffi::furi_record_exists(self.tag().as_ptr() as _) } }
}


pub struct Storage;

impl const Record for Storage {
	const LEN: usize = ffi::RECORD_STORAGE.len();
	fn tag(&self) -> &[u8; Self::LEN] { ffi::RECORD_STORAGE }
}

impl RecordExt for Storage {}


pub(crate) struct CloseOnDrop<T>(T)
	where T: Record,
	      [(); T::LEN]: Sized;

impl<T: Record> Drop for CloseOnDrop<T> where [(); T::LEN]: Sized {
	fn drop(&mut self) { unsafe { ffi::furi_record_close(self.0.tag().as_ptr() as _) } }
}

impl<T> CloseOnDrop<T>
	where T: Record,
	      [(); T::LEN]: Sized
{
	#[inline(always)]
	pub fn new(rec: T) -> Self { Self(rec) }
}