gneiss 0.2.0

Safe Rust SDK for Pebble watchapps and watchfaces
Documentation
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Persistent storage for data between app launches.
//!
//! Every key is declared with [`persist_keys!`](crate::persist_keys), once per app, which numbers
//! them so no two collide and gives each its type.
//!
//! Each key is its own flash write and is atomic. Separate keys have their own r/w cycles and their
//! own headers (~12B). Writing a large struct repeatedly to change one field is hard-wearing on the
//! flash, however - so consider keeping oft-changed settings in their own keys.
//! General structs can be atomically saved/loaded via [`make_persistible`](crate::make_persistible),
//! or any other [`Pod`] value through [`AsBytes`].
//!
//! ```no_run,standalone_crate
//! use gneiss::resource::persist::{Persist, Recover};
//!
//! gneiss::persist_keys! {
//!     SETTINGS_VOLUME: i32,
//!     SETTINGS_PALETTE: i32,
//!     SETTINGS_VIBRATE: bool,
//!     LAUNCHES: i32,
//!     HIGH_SCORES: HighScores,
//!     /// How many notes are saved: a tail has no end of its own, so keep track of what you wrote
//!     NOTE_COUNT: i32,
//!     /// One key per saved note, as many as the app saves
//!     NOTES: [Note; ..],
//! }
//!
//! #[gneiss::make_persistible]
//! struct HighScores {
//!     top: [u32; 5],
//! }
//!
//! #[gneiss::make_persistible]
//! struct Note {
//!     created: u32,
//!     text:    [u8; 60],
//! }
//!
//! fn set_volume(volume: i32) {
//!     // Saves this setting alone
//!     let _ = volume.persist(SETTINGS_VOLUME);
//! }
//!
//! fn launched() {
//!     // Unset reads as 0
//!     let _ = (i32::recover_or_default(LAUNCHES) + 1).persist(LAUNCHES);
//! }
//!
//! fn scored(score: u32) {
//!     // Highest first
//!     let mut scores = HighScores::recover(HIGH_SCORES).unwrap_or(HighScores { top: [0; 5] });
//!     if let Some(rank) = scores.top.iter().position(|&top| score > top) {
//!         // Shift the lower scores down, dropping the last
//!         let last = scores.top.len() - 1;
//!         scores.top.copy_within(rank..last, rank + 1);
//!         scores.top[rank] = score;
//!         // The whole table, in one write
//!         let _ = scores.persist(HIGH_SCORES);
//!     }
//! }
//!
//! fn add_note(note: Note) {
//!     let count = i32::recover_or_default(NOTE_COUNT);
//!     if let Some(key) = NOTES.get(count as u32)
//!         && note.persist(key).is_ok()
//!     {
//!         let _ = (count + 1).persist(NOTE_COUNT);
//!     }
//! }
//!
//! fn notes() -> impl Iterator<Item = Note> {
//!     (0..i32::recover_or_default(NOTE_COUNT) as u32)
//!         .filter_map(|index| Note::recover(NOTES.get(index)?))
//! }
//! ```

use core::marker::PhantomData;

use bytemuck::Pod;

use crate::{status::StatusError, sys};

/// How many bytes of persistent storage the app may use: 4 KB on the frozen-SDK watches, and what
/// the firmware reports on the rest. Each key costs about 12 bytes of overhead.
pub fn max_persist() -> usize {
	#[cfg(pbl_sdk_frozen)]
	return 4096;
	#[cfg(not(pbl_sdk_frozen))]
	unsafe {
		sys::persist_get_max_size()
	}
}

/// A key holding a `T`, declared with [`persist_keys!`](crate::persist_keys).
pub struct PersistKey<T: Persist + ?Sized> {
	key:  u32,
	data: PhantomData<T>,
}

impl<T: Persist + ?Sized> Clone for PersistKey<T> {
	fn clone(&self) -> Self {
		*self
	}
}
/// Ignore the Copy-ness of T, since access to the T is a Cloning-load from the firmware and copying
/// this handle is like copying a ref
impl<T: Persist + ?Sized> Copy for PersistKey<T> {}

impl<T: Persist + ?Sized> PersistKey<T> {
	/// Called by the code [`persist_keys!`](crate::persist_keys) generates.
	#[doc(hidden)]
	pub const fn __new(key: u32) -> Self {
		Self {
			key,
			data: PhantomData,
		}
	}

	/// Read the data in this slot as `U`, analogous to `ptr::cast()`. Useful for migrating data
	/// from older versiond of your app.
	/// Reading a key as another type than it holds can't cause undefined behaviour, but can return
	/// a garbage value or error if T -> U is not a valid cast.
	pub const fn retype<U: Persist + ?Sized>(self) -> PersistKey<U> {
		PersistKey::__new(self.key)
	}

	/// The raw key the firmware stores under.
	pub const fn key(self) -> u32 {
		self.key
	}

	/// Whether the key holds a value. An empty one doesn't count: the firmware stores none.
	pub fn exists(self) -> bool {
		unsafe { sys::persist_exists(self.key) }
	}

	/// Ignores firmware return, no-ops "not set"
	pub fn delete(self) {
		unsafe { sys::persist_delete(self.key) };
	}

	/// How many bytes the key holds. `DoesNotExist` if unset.
	pub fn size(self) -> Result<u32, StatusError> {
		StatusError::check(unsafe { sys::persist_get_size(self.key) })
	}
}

/// Store a value in persistent storage.
pub trait Persist {
	/// `Ok(bytes written)`, or `StatusError`
	fn persist(&self, key: PersistKey<Self>) -> Result<u32, StatusError>;
}

/// Read back a value of a fixed size.
///
/// Stored data outlives the app version that wrote it, so a key can hold bytes written as any type.
/// An implementation must be sound whatever bytes it reads back: it may return a meaningless value,
/// but never an invalid one. [`PersistKey::retype`] relies on this too.
pub trait Recover: Persist + Sized {
	/// The value at `key`, or `None` if it's unset.
	fn recover(key: PersistKey<Self>) -> Option<Self>;

	/// The value at `key`, or [`Default`] if it's unset. Skips telling the two apart where the
	/// firmware's own answer for an unset key is already the default, so it can do less work than
	/// [`recover`](Self::recover).
	fn recover_or_default(key: PersistKey<Self>) -> Self
	where
		Self: Default,
	{
		Self::recover(key).unwrap_or_default()
	}
}

/// Read back a value without a fixed size, into the caller's buffer. Like [`Recover`], an
/// implementation must be sound whatever bytes it reads back.
pub trait RecoverSlice: Persist {
	/// The value at `key`, read into `buf`, as a view of it and the bytes read. `DoesNotExist` if
	/// unset. A value longer than `buf` is cut short.
	fn recover(key: PersistKey<Self>, buf: &mut [u8]) -> Result<&Self, StatusError>;
}

impl Persist for bool {
	fn persist(&self, key: PersistKey<Self>) -> Result<u32, StatusError> {
		StatusError::check(unsafe { sys::persist_write_bool(key.key, *self) })
	}
}

impl Recover for bool {
	fn recover(key: PersistKey<Self>) -> Option<Self> {
		key.exists().then(|| Self::recover_or_default(key))
	}

	/// One read: an unset key reads as `false`.
	fn recover_or_default(key: PersistKey<Self>) -> Self {
		// Not persist_read_bool: it hands back the first stored byte as-is, which is not a valid
		// rust `bool` if the key holds anything but one. This is the idea, but truncating
		let mut byte = 0u8;
		unsafe { sys::persist_read_data(key.key, (&raw mut byte).cast(), 1) };
		byte != 0
	}
}

impl Persist for i32 {
	fn persist(&self, key: PersistKey<Self>) -> Result<u32, StatusError> {
		StatusError::check(unsafe { sys::persist_write_int(key.key, *self) })
	}
}
impl Recover for i32 {
	fn recover(key: PersistKey<Self>) -> Option<Self> {
		key.exists().then(|| Self::recover_or_default(key))
	}

	/// One read: the firmware gives 0 for an unset key.
	fn recover_or_default(key: PersistKey<Self>) -> Self {
		unsafe { sys::persist_read_int(key.key) }
	}
}

/// Writing an empty slice deletes the key: it reads back as unset.
impl Persist for [u8] {
	fn persist(&self, key: PersistKey<Self>) -> Result<u32, StatusError> {
		if self.len() <= sys::PERSIST_DATA_MAX_LENGTH as usize {
			StatusError::check(unsafe {
				sys::persist_write_data(key.key, self.as_ptr().cast(), self.len())
			})
		} else {
			Err(StatusError::OutOfRange)
		}
	}
}

impl RecoverSlice for [u8] {
	fn recover(key: PersistKey<Self>, buf: &mut [u8]) -> Result<&Self, StatusError> {
		let bytes = StatusError::check(unsafe {
			sys::persist_read_data(key.key, buf.as_mut_ptr().cast(), buf.len())
		})?;
		// The firmware reads at most buf.len()
		let read = buf.get(..bytes as usize).ok_or(StatusError::Internal)?;
		Ok(read)
	}
}

impl Persist for core::ffi::CStr {
	fn persist(&self, key: PersistKey<Self>) -> Result<u32, StatusError> {
		if self.count_bytes() < sys::PERSIST_STRING_MAX_LENGTH as usize {
			StatusError::check(unsafe { sys::persist_write_string(key.key, self.as_ptr()) })
		} else {
			Err(StatusError::OutOfRange)
		}
	}
}

impl RecoverSlice for core::ffi::CStr {
	/// `Range` if `buf` is empty, with no room for the NUL. The bytes read include the NUL.
	fn recover(key: PersistKey<Self>, buf: &mut [u8]) -> Result<&Self, StatusError> {
		// The firmware terminates the string by writing buf[bytes - 1], unchecked
		if buf.is_empty() {
			return Err(StatusError::OutOfRange);
		}
		let bytes = StatusError::check(unsafe {
			sys::persist_read_string(key.key, buf.as_mut_ptr().cast(), buf.len())
		})?;
		let read = buf.get(..bytes as usize).ok_or(StatusError::Internal)?;
		// The firmware NUL-terminated it
		let string =
			core::ffi::CStr::from_bytes_until_nul(read).map_err(|_| StatusError::Internal)?;
		Ok(string)
	}
}

/// A [`Pod`] value stored as its bytes, in one key: one write, so it can't be left half updated.
/// At most [`PERSIST_DATA_MAX_LENGTH`](sys::PERSIST_DATA_MAX_LENGTH) bytes, checked at compile time.
/// For a struct of your own, [`make_persistible`](crate::make_persistible) does the same without the
/// wrapper.
///
/// Reading back stored bytes is only sound because every bit pattern of a `Pod` type is valid. A
/// change to `T`'s size between app versions reads back as unset.
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct AsBytes<T: Pod>(pub T);

impl<T: Pod> Default for AsBytes<T> {
	fn default() -> Self {
		Self(T::zeroed())
	}
}

impl<T: Pod> Persist for AsBytes<T> {
	fn persist(&self, key: PersistKey<Self>) -> Result<u32, StatusError> {
		__persist_pod(&self.0, key.key)
	}
}

impl<T: Pod> Recover for AsBytes<T> {
	/// `None` if unset, or stored at another size.
	fn recover(key: PersistKey<Self>) -> Option<Self> {
		__recover_pod(key.key).map(Self)
	}
}

/// Write `value`'s bytes to `key`. Called by [`AsBytes`] and the code
/// [`make_persistible`](crate::make_persistible) generates.
#[doc(hidden)]
pub fn __persist_pod<T: Pod>(value: &T, key: u32) -> Result<u32, StatusError> {
	const {
		assert!(
			size_of::<T>() <= sys::PERSIST_DATA_MAX_LENGTH as usize,
			"persisted value is over PERSIST_DATA_MAX_LENGTH"
		);
	}
	let bytes = bytemuck::bytes_of(value);
	StatusError::check(unsafe { sys::persist_write_data(key, bytes.as_ptr().cast(), bytes.len()) })
}

/// The `T` stored at `key`. `None` if it's unset or stored at another size. Called by [`AsBytes`]
/// and the code [`make_persistible`](crate::make_persistible) generates.
#[doc(hidden)]
pub fn __recover_pod<T: Pod>(key: u32) -> Option<T> {
	let size = size_of::<T>();
	if usize::try_from(unsafe { sys::persist_get_size(key) }) != Ok(size) {
		return None;
	}
	let mut value = T::zeroed();
	let bytes = bytemuck::bytes_of_mut(&mut value);
	let read = unsafe { sys::persist_read_data(key, bytes.as_mut_ptr().cast(), size) };
	(usize::try_from(read) == Ok(size)).then_some(value)
}

/// A run of keys each holding a `T`, declared as `NAME: [T; len]` in
/// [`persist_keys!`](crate::persist_keys).
pub struct PersistBlock<T: Persist + ?Sized> {
	first: u32,
	len:   u32,
	data:  PhantomData<T>,
}

impl<T: Persist + ?Sized> Clone for PersistBlock<T> {
	fn clone(&self) -> Self {
		*self
	}
}
impl<T: Persist + ?Sized> Copy for PersistBlock<T> {}

impl<T: Persist + ?Sized> PersistBlock<T> {
	/// Called by the code [`persist_keys!`](crate::persist_keys) generates.
	#[doc(hidden)]
	pub const fn __new(first: u32, len: u32) -> Self {
		Self {
			first,
			len,
			data: PhantomData,
		}
	}

	/// How many keys the block holds.
	pub const fn len(self) -> u32 {
		self.len
	}

	pub const fn is_empty(self) -> bool {
		self.len == 0
	}

	/// The `index`th key, or `None` past the end.
	pub const fn get(self, index: u32) -> Option<PersistKey<T>> {
		if index < self.len {
			Some(PersistKey::__new(self.first + index))
		} else {
			None
		}
	}

	/// Every key in the block, in order.
	pub fn keys(self) -> impl Iterator<Item = PersistKey<T>> {
		(self.first..self.first + self.len).map(PersistKey::__new)
	}
}

/// Every key after the rest each holding a `T`, declared last as `NAME: [T; ..]` in
/// [`persist_keys!`](crate::persist_keys), for data saved under as many keys as it needs.
pub struct PersistTail<T: Persist + ?Sized> {
	first: u32,
	data:  PhantomData<T>,
}

impl<T: Persist + ?Sized> Clone for PersistTail<T> {
	fn clone(&self) -> Self {
		*self
	}
}
impl<T: Persist + ?Sized> Copy for PersistTail<T> {}

impl<T: Persist + ?Sized> PersistTail<T> {
	/// Called by the code [`persist_keys!`](crate::persist_keys) generates.
	#[doc(hidden)]
	pub const fn __new(first: u32) -> Self {
		Self {
			first,
			data: PhantomData,
		}
	}

	/// The `index`th key, or `None` past the end of the key space.
	pub const fn get(self, index: u32) -> Option<PersistKey<T>> {
		match self.first.checked_add(index) {
			Some(key) => Some(PersistKey::__new(key)),
			None => None,
		}
	}
}