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
use crate::{
	standard_c::{CStr, NotStack},
	Box, Handle, SpecialDrop,
};
use core::{
	marker::PhantomData,
	mem::ManuallyDrop,
	ops::{Deref, DerefMut},
};
#[allow(clippy::wildcard_imports)]
use pebble_sys::{
	prelude::*,
	standard_c::memory::void,
	user_interface::window::number_window::{NumberWindow as sysNumberWindow, *},
};

use super::{WindowRef, WindowRefMut};

pub struct NumberWindow<'a, T: ?Sized>(
	pub(crate) Handle<'a, sysNumberWindow<'a>>,
	PhantomData<T>,
	*mut NumberWindowDataWrapper<'a>,
);

pub struct NumberWindowData<
	I: FnMut(&NumberWindow<void>, &mut T),
	D: FnMut(&NumberWindow<void>, &mut T),
	S: FnMut(&NumberWindow<void>, &mut T),
	T,
> {
	pub incremented: I,
	pub decremented: D,
	pub selected: S,
	pub context: T,
}

trait NumberWindowDataTrait {
	fn incremented(&mut self, number_window: &NumberWindow<void>);
	fn decremented(&mut self, number_window: &NumberWindow<void>);
	fn selected(&mut self, number_window: &NumberWindow<void>);
}

impl<
		I: FnMut(&NumberWindow<void>, &mut T),
		D: FnMut(&NumberWindow<void>, &mut T),
		S: FnMut(&NumberWindow<void>, &mut T),
		T,
	> NumberWindowDataTrait for NumberWindowData<I, D, S, T>
{
	fn incremented(&mut self, number_window: &NumberWindow<void>) {
		(self.incremented)(number_window, &mut self.context)
	}

	fn decremented(&mut self, number_window: &NumberWindow<void>) {
		(self.decremented)(number_window, &mut self.context)
	}

	fn selected(&mut self, number_window: &NumberWindow<void>) {
		(self.selected)(number_window, &mut self.context)
	}
}

pub struct NumberWindowDataWrapper<'a>(Box<'a, dyn 'a + NumberWindowDataTrait>);

impl<'a, T> NumberWindow<'a, T> {
	// TODO: This probably should take and set a set of window handlers, which can then also act as lifecycle hooks for the context.
	/// # Errors
	///
	/// TODO
	///
	pub fn new<
		I: 'a + FnMut(&NumberWindow<void>, &mut T),
		D: 'a + FnMut(&NumberWindow<void>, &mut T),
		S: 'a + FnMut(&NumberWindow<void>, &mut T),
	>(
		label: &'a CStr<impl NotStack>,
		number_window_data: NumberWindowData<I, D, S, T>,
	) -> Result<Self, NumberWindowData<I, D, S, T>>
	where
		T: 'a,
	{
		#![allow(clippy::items_after_statements)]

		let window_data_wrapper = Box::leak(
			Box::new(NumberWindowDataWrapper(Box::new(number_window_data)?)).map_err(
				|wrapper| Box::into_inner(unsafe { Box::downcast_unchecked(wrapper.0) }),
			)?,
		) as *mut NumberWindowDataWrapper;

		extern "C" fn raw_incremented<'a>(
			raw_window: &'a mut sysNumberWindow<'a>,
			context: &mut void,
		) {
			let context = context as *mut void; // This will be aliased.
			let fake_window = unsafe {
				//SAFETY: It's actually *kind of* safe to alias NumberWindow instances... But only because they store a Handle internally, which stores a pointer.
				// Actually accessing associated data would NOT be safe, so the user-provided handlers only see a NumberWindow<void> where such access is impossible.
				#[allow(clippy::cast_ptr_alignment)]
				NumberWindow::<void>::from_raw_unsized(
					raw_window,
					context as *mut _ as *mut NumberWindowDataWrapper,
				)
			};
			unsafe {
				//SAFETY: And here's the third concurrent use of this pointer.
				// The reference goes out of scope before the others are used, so this is safe.
				let context = &mut *context;
				context
					.cast_unchecked_mut::<NumberWindowDataWrapper>()
					.0
					.incremented(&fake_window)
			}
			fake_window.abandon();
		}
		extern "C" fn raw_decremented<'a>(
			raw_window: &'a mut sysNumberWindow<'a>,
			context: &mut void,
		) {
			let context = context as *mut void; // This will be aliased.
			let fake_window = unsafe {
				//SAFETY: It's actually *kind of* safe to alias NumberWindow instances... But only because they store a Handle internally, which stores a pointer.
				// Actually accessing associated data would NOT be safe, so the user-provided handlers only see a NumberWindow<void> where such access is impossible.
				#[allow(clippy::cast_ptr_alignment)]
				NumberWindow::<void>::from_raw_unsized(
					raw_window,
					context as *mut _ as *mut NumberWindowDataWrapper,
				)
			};
			unsafe {
				//SAFETY: And here's the third concurrent use of this pointer.
				// The reference goes out of scope before the others are used, so this is safe.
				let context = &mut *context;
				context
					.cast_unchecked_mut::<NumberWindowDataWrapper>()
					.0
					.decremented(&fake_window)
			}
			fake_window.abandon();
		}
		extern "C" fn raw_selected<'a>(
			raw_window: &'a mut sysNumberWindow<'a>,
			context: &mut void,
		) {
			let context = context as *mut void; // This will be aliased.
			let fake_window = unsafe {
				//SAFETY: It's actually *kind of* safe to alias NumberWindow instances... But only because they store a Handle internally, which stores a pointer.
				// Actually accessing associated data would NOT be safe, so the user-provided handlers only see a NumberWindow<void> where such access is impossible.
				#[allow(clippy::cast_ptr_alignment)]
				NumberWindow::<void>::from_raw_unsized(
					raw_window,
					context as *mut _ as *mut NumberWindowDataWrapper,
				)
			};
			unsafe {
				//SAFETY: And here's the third concurrent use of this pointer.
				// The reference goes out of scope before the others are used, so this is safe.
				let context = &mut *context;
				context
					.cast_unchecked_mut::<NumberWindowDataWrapper>()
					.0
					.selected(&fake_window)
			}
			fake_window.abandon();
		}

		match unsafe {
			number_window_create(
				label.as_c_str(),
				NumberWindowCallbacks {
					incremented: Some(raw_incremented),
					decremented: Some(raw_decremented),
					selected: Some(raw_selected),
				},
				&mut *(window_data_wrapper as *mut _ as *mut void),
			)
		} {
			Some(raw_window) => Ok(Self(
				Handle::new(raw_window),
				PhantomData,
				window_data_wrapper,
			)),
			None => Err(Box::into_inner(unsafe {
				Box::downcast_unchecked(
					Box::into_inner(Box::<NumberWindowDataWrapper>::from_raw(
						&mut *window_data_wrapper,
					))
					.0,
				)
			})),
		}
	}

	/// Assembles a new instance of [`NumberWindow`] from the given raw window handle.
	///
	/// # Safety
	///
	/// This function is only safe if `raw_window` is a raw window handle that was previously [`.leak()`]ed from the same [`NumberWindow<T>`] variant and no other [`NumberWindow<T>`] instance has been created from it since.
	///
	/// [`.leak()`]: #method.leak
	pub unsafe fn from_raw(
		raw_window: &'a mut sysNumberWindow<'a>,
		number_window_data_wrapper: *mut NumberWindowDataWrapper<'a>,
	) -> Self {
		Self(
			Handle::new(raw_window),
			PhantomData,
			number_window_data_wrapper,
		)
	}

	/// Leaks the current [`NumberWindow`] instance into a raw Pebble number window handle.
	///
	/// Note that [`NumberWindow`] has associated heap instances beyond the raw window, so only destroying that would still leak memory.
	#[must_use = "Not reassembling the `NumberWindow` later causes a memory leak."]
	pub fn leak(
		self,
	) -> (
		&'a mut sysNumberWindow<'a>,
		*mut NumberWindowDataWrapper<'a>,
	)
	where
		T: 'a,
	{
		let undropped = ManuallyDrop::new(self);
		unsafe { (undropped.0.duplicate().unwrap(), undropped.2) }
	}
}

impl<'a, T: ?Sized> NumberWindow<'a, T> {
	#[must_use]
	pub fn window(&self) -> WindowRef<'_> {
		WindowRef(Handle::new(unsafe {
			number_window_get_window_mut(&mut *(self.0.as_mut_unchecked() as *mut _))
		}))
	}

	#[must_use]
	pub fn window_mut<'b: 'a>(&'b mut self) -> WindowRefMut<'b> {
		WindowRefMut(Handle::new(unsafe {
			number_window_get_window_mut(self.0.as_mut_unchecked())
		}))
	}

	pub fn set_label(&self, label: &'a CStr<impl NotStack>) {
		unsafe { number_window_set_label(self.0.as_mut_unchecked(), label.as_c_str()) }
	}

	pub fn set_max(&self, max: i32) {
		unsafe { number_window_set_max(self.0.as_mut_unchecked(), max) }
	}

	pub fn set_min(&self, min: i32) {
		unsafe { number_window_set_min(self.0.as_mut_unchecked(), min) }
	}

	pub fn set_value(&self, value: i32) {
		unsafe { number_window_set_value(self.0.as_mut_unchecked(), value) }
	}

	pub fn set_step_size(&self, step_size: i32) {
		unsafe { number_window_set_step_size(self.0.as_mut_unchecked(), step_size) }
	}

	#[must_use]
	pub fn get_value(&self) -> i32 {
		unsafe { number_window_get_value(&*self.0) }
	}

	/// # Safety
	///
	/// It's actually safe to assemble [`NumberWindow`] instances with mismatched type parameters iff the type parameter assembled against is unsized,
	/// because this data can never be directly accessed outside the destructor.
	///
	/// However, dropping such a value will always panic, so adding this to the public API would be a *really* bad idea.
	unsafe fn from_raw_unsized(
		raw_window: &'a mut sysNumberWindow<'a>,
		number_window_data_wrapper: *mut NumberWindowDataWrapper<'a>,
	) -> Self {
		Self(
			Handle::new(raw_window),
			PhantomData,
			number_window_data_wrapper,
		)
	}

	/// Discards this instance while skipping the destructor. Helper for aliased temporaries.
	fn abandon(self) {
		let _ = ManuallyDrop::new(self);
	}
}

impl<'a, T> Deref for NumberWindow<'a, T> {
	type Target = NumberWindow<'a, void>;

	fn deref(&self) -> &Self::Target {
		unsafe {
			//SAFETY: Same memory layout, no access to data.
			&*(self as *const _ as *const Self::Target)
		}
	}
}

impl<'a, T> DerefMut for NumberWindow<'a, T> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		unsafe {
			//SAFETY: Same memory layout, no access to data.
			&mut *(self as *mut _ as *mut Self::Target)
		}
	}
}

impl<'a, T: ?Sized> Drop for NumberWindow<'a, T> {
	fn drop(&mut self) {
		self.special_drop()
	}
}

impl<'a, T: ?Sized> SpecialDrop for NumberWindow<'a, T> {
	default fn special_drop(&mut self) {
		panic!("Dropping unsized `NumberWindow<T>`s is illegal")
	}
}

impl<'a, T: Sized> SpecialDrop for NumberWindow<'a, T> {
	fn special_drop(&mut self) {
		unsafe {
			//SAFETY: window_data is created and leaked in the only accessible constructor.
			//SAFETY: self.0 isn't accessed after this.
			let data_wrapper = self.2;
			// Detaching the lifetime here takes a bit of work.
			let sys_number_window = self.0.duplicate().unwrap() as *mut _ as *mut void as *mut _;

			// Destroy the window, THEN drop its data.
			number_window_destroy(&mut *sys_number_window);
			Box::<NumberWindowDataWrapper>::from_raw(&mut *data_wrapper);
		}
	}
}