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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Types representing states
//!
//! Additional `impl` blocks for these types can be found in other modules of this crate.

#![deny(unsafe_code)]

use std::fmt;
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ops::Deref;

use crate::state_name::StateName;
use crate::type_id::{TypeId, GUID};

/// An owned state
///
/// This deletes the represented state on drop. You can prevent this behavior by calling the
/// [`leak`](OwnedState::leak) method.
///
/// While ownership in Rust usually refers to the ownership of memory, this applies the idea of ownership to an
/// external entity, namely a state. It's similar to [`OwnedHandle`](std::os::windows::io::OwnedHandle) in that
/// regard.
///
/// An [`OwnedState<T>`] can be borrowed as a [`BorrowedState<'_, T>`](BorrowedState) using the [`AsState::as_state`]
/// method.
///
/// The type parameter `T` is the type of data contained in the state.
pub struct OwnedState<T>
where
    T: ?Sized,
{
    pub(crate) raw: RawState<T>,
}

impl<T> OwnedState<T>
where
    T: ?Sized,
{
    /// Returns the name of this state
    pub const fn state_name(&self) -> StateName {
        self.raw.state_name()
    }

    /// Leaks this [`OwnedState<T>`]
    ///
    /// This consumes the [`OwnedState<T>`] without dropping it, returning a [`BorrowedState<'static,
    /// T>`](BorrowedState) with `'static` lifetime that represents the same underlying state.
    ///
    /// Note that while this is named after [`Box::leak`], it doesn't leak memory, but it leaks a state in the sense
    /// that the state doesn't get deleted on drop.
    pub fn leak(self) -> BorrowedState<'static, T> {
        BorrowedState::from_raw(self.into_raw())
    }

    /// Casts the data type of this state to a different type `U`
    ///
    /// The returned [`OwnedState<U>`] represents the same underlying state, but treats it as containing data of
    /// a different type `U`.
    pub fn cast<U>(self) -> OwnedState<U>
    where
        U: ?Sized,
    {
        OwnedState::from_raw(self.into_raw().cast())
    }

    /// Creates a new [`OwnedState`] wrapping a given [`RawState`]
    pub(crate) const fn from_raw(raw: RawState<T>) -> Self {
        Self { raw }
    }

    /// Consumes this [`OwnedState`] without dropping it, returning the inner [`RawState`]
    pub(crate) fn into_raw(self) -> RawState<T> {
        ManuallyDrop::new(self).raw
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: PartialEq<T>`
impl<T> PartialEq for OwnedState<T>
where
    T: ?Sized,
{
    fn eq(&self, other: &Self) -> bool {
        self.raw == other.raw
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: Eq`
impl<T> Eq for OwnedState<T> where T: ?Sized {}

// We cannot derive this because that would impose an unnecessary trait bound `T: Hash`
impl<T> Hash for OwnedState<T>
where
    T: ?Sized,
{
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.raw.hash(state);
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: Debug`
impl<T> Debug for OwnedState<T>
where
    T: ?Sized,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("OwnedState").field("raw", &self.raw).finish()
    }
}

impl<T> Drop for OwnedState<T>
where
    T: ?Sized,
{
    fn drop(&mut self) {
        let _ = self.raw.delete();
    }
}

/// A borrowed state
///
/// This has a lifetime parameter to tie it to something that owns the state, typically an [`OwnedState<T>`].
///
/// Unlike [`OwnedState<T>`], this implements [`Copy`] (and [`Clone`]) and does not delete the represented state on
/// drop.
///
/// While borrowing in Rust usually refers to borrowing memory, this applies the idea of borrowing to an external
/// entity, namely a state. It's similar to [`BorrowedHandle<'_>`](std::os::windows::io::BorrowedHandle) in that
/// regard.
///
/// Calling [`Clone::clone`] on a [`BorrowedState<'a, T>`](BorrowedState) just makes a trivial copy, returning another
/// [`BorrowedState<'a, T>`](BorrowedState) with the same lifetime as the original one and representing the same
/// underlying WNF state. The same applies to the [`ToOwned::to_owned`] method.  If you want to turn a
/// [`BorrowedState<'_, T>`](BorrowedState) into an [`OwnedState<T>`] (which will then delete the underlying state on
/// drop), use the [`BorrowedState::to_owned_state`] method.
///
/// The type parameter `T` is the type of data contained in the state.
pub struct BorrowedState<'a, T>
where
    T: ?Sized,
{
    pub(crate) raw: RawState<T>,
    _marker: PhantomData<&'a ()>,
}

impl<'a, T> BorrowedState<'a, T>
where
    T: ?Sized,
{
    /// Returns the name of this state
    pub const fn state_name(self) -> StateName {
        self.raw.state_name()
    }

    /// Turns this [`BorrowedState<'_, T>`](BorrowedState) into an [`OwnedState<T>`] representing the same underlying
    /// state
    ///
    /// Note that the underlying state will be deleted when the [`OwnedState<T>`] is dropped.
    pub const fn to_owned_state(self) -> OwnedState<T> {
        OwnedState::from_raw(self.raw)
    }

    /// Casts the data type of this state to a different type `U`
    ///
    /// The returned [`BorrowedState<'a, U>`](BorrowedState) represents the same underlying state, but treats it as
    /// containing data of a different type `U`.
    pub const fn cast<U>(self) -> BorrowedState<'a, U>
    where
        U: ?Sized,
    {
        BorrowedState::from_raw(self.raw.cast())
    }

    /// Creates a new [`BorrowedState<'_, T>`](BorrowedState) wrapping a given [`RawState<T>`]
    ///
    /// The lifetime `'a` of the returned [`BorrowedState<'a, T>`](BorrowedState) is inferred at the call site.
    pub(crate) const fn from_raw(raw: RawState<T>) -> Self {
        Self {
            raw,
            _marker: PhantomData,
        }
    }
}

impl<T> BorrowedState<'static, T>
where
    T: ?Sized,
{
    /// Statically borrows the state with the given name
    ///
    /// Note that an underlying state with the given name may or may not exist. The returned
    /// [`BorrowedState<'static, T>`](BorrowedState) having a `'static` lifetime just means that the state is borrowed
    /// directly from the system rather than from an [`OwnedState<T>`] that will be dropped at some point.
    pub fn from_state_name(state_name: impl Into<StateName>) -> Self {
        Self::from_raw(RawState::from_state_name_and_type_id(state_name.into(), TypeId::none()))
    }

    /// Statically borrows the state with the given name using the given type id
    ///
    /// Note that an underlying state with the given name may or may not exist. The returned
    /// [`BorrowedState<'static, T>`](BorrowedState) having a `'static` lifetime just means that the state is borrowed
    /// directly from the system rather than from an [`OwnedState<T>`] that will be dropped at some point.
    pub fn from_state_name_and_type_id(state_name: impl Into<StateName>, type_id: impl Into<GUID>) -> Self {
        Self::from_raw(RawState::from_state_name_and_type_id(
            state_name.into(),
            TypeId::from_guid(type_id.into()),
        ))
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: Copy`
impl<T> Copy for BorrowedState<'_, T> where T: ?Sized {}

// We cannot derive this because that would impose an unnecessary trait bound `T: Clone`
impl<T> Clone for BorrowedState<'_, T>
where
    T: ?Sized,
{
    fn clone(&self) -> Self {
        *self
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: PartialEq<T>`
impl<T> PartialEq for BorrowedState<'_, T>
where
    T: ?Sized,
{
    fn eq(&self, other: &Self) -> bool {
        self.raw == other.raw
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: Eq`
impl<T> Eq for BorrowedState<'_, T> where T: ?Sized {}

// We cannot derive this because that would impose an unnecessary trait bound `T: Hash`
impl<T> Hash for BorrowedState<'_, T>
where
    T: ?Sized,
{
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.raw.hash(state);
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: Debug`
impl<T> Debug for BorrowedState<'_, T>
where
    T: ?Sized,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("BorrowedState").field("raw", &self.raw).finish()
    }
}

/// A trait for types that can be borrowed as a state
///
/// This is implemented for both [`OwnedState<T>`] and [`BorrowedState<'_, T>`](BorrowedState). There are two main use
/// cases for it:
///
/// - Functions that can accept either a reference to an owned or a borrowed state: Even though a
/// [`BorrowedState<'_, T>`](BorrowedState) plays the role of a reference to a state, it's not technically a reference.
/// As a consequence, there is no deref coercion for states, i.e. you cannot just pass an [`&'a
/// OwnedState<T>`](OwnedState) where a [`BorrowedState<'a, T>`](BorrowedState) is expected. In order to accept both
/// types, functions can instead take a reference to a generic type implementing [`AsState`]:
/// ```
/// # use std::io;
/// # use wnf::{AsState, OwnedState};
/// #
/// fn add_to_state(state: &impl AsState<Data = u32>, delta: u32) -> io::Result<()> {
///     let state = state.as_state();
///     let value = state.get()?;
///     let new_value = value + delta;
///     state.set(&new_value)
/// }
/// ```
///
/// - Types that can contain either an owned or a borrowed state:
/// ```
/// # use std::io;
/// # use wnf::AsState;
/// #
/// struct StateWrapper<S> {
///     state: S,
/// }
///
/// impl<S> StateWrapper<S>
/// where
///     S: AsState<Data = u32>,
/// {
///     fn add(&self, delta: u32) -> io::Result<()> {
///         let state = self.state.as_state();
///         let value = state.get()?;
///         let new_value = value + delta;
///         state.set(&new_value)
///     }
/// }
/// ```
///
/// When comparing [`OwnedState<T>`] with [`OwnedHandle`](std::os::windows::io::OwnedHandle) and
/// [`BorrowedState<'_, T>`](BorrowedState) with [`BorrowedHandle<'_>`](std::os::windows::io::BorrowedHandle), this
/// trait plays the role of the [`AsHandle`](std::os::windows::io::AsHandle) trait.
///
/// This trait is sealed and cannot by implemented outside of the `wnf` crate.
pub trait AsState: private::Sealed {
    /// The type of the data contained in the borrowed state
    type Data: ?Sized;

    /// Borrows a value as a state
    fn as_state(&self) -> BorrowedState<'_, Self::Data>;
}

impl<T> AsState for OwnedState<T>
where
    T: ?Sized,
{
    type Data = T;

    fn as_state(&self) -> BorrowedState<'_, T> {
        BorrowedState::from_raw(self.raw)
    }
}

impl<T> AsState for BorrowedState<'_, T>
where
    T: ?Sized,
{
    type Data = T;

    fn as_state(&self) -> BorrowedState<'_, T> {
        *self
    }
}

impl<S> AsState for S
where
    S: Deref,
    S::Target: AsState,
{
    type Data = <S::Target as AsState>::Data;

    fn as_state(&self) -> BorrowedState<'_, Self::Data> {
        self.deref().as_state()
    }
}

/// A raw state
///
/// This neither deletes the underlying state on drop, nor does it have a lifetime.
pub(crate) struct RawState<T>
where
    T: ?Sized,
{
    pub(crate) state_name: StateName,
    pub(crate) type_id: TypeId,
    // `RawState<T>` is neither covariant nor contravariant in `T` and doesn't own a `T`
    _marker: PhantomData<fn(T) -> T>,
}

impl<T> RawState<T>
where
    T: ?Sized,
{
    /// Creates a [`RawState<T>`] with the given name using the given type id
    pub(crate) const fn from_state_name_and_type_id(state_name: StateName, type_id: TypeId) -> Self {
        Self {
            state_name,
            type_id,
            _marker: PhantomData,
        }
    }

    /// Returns the name of this state
    const fn state_name(self) -> StateName {
        self.state_name
    }

    /// Casts the data type of this state to a different type `U`
    ///
    /// The returned [`RawState<U>`] represents the same underlying state, but treats it as containing data of
    /// a different type `U`.
    pub(crate) const fn cast<U>(self) -> RawState<U>
    where
        U: ?Sized,
    {
        RawState::from_state_name_and_type_id(self.state_name, self.type_id)
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: Copy`
impl<T> Copy for RawState<T> where T: ?Sized {}

// We cannot derive this because that would impose an unnecessary trait bound `T: Clone`
impl<T> Clone for RawState<T>
where
    T: ?Sized,
{
    fn clone(&self) -> Self {
        *self
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: PartialEq<T>`
impl<T> PartialEq for RawState<T>
where
    T: ?Sized,
{
    fn eq(&self, other: &Self) -> bool {
        self.state_name == other.state_name && self.type_id == other.type_id
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: Eq`
impl<T> Eq for RawState<T> where T: ?Sized {}

// We cannot derive this because that would impose an unnecessary trait bound `T: Hash`
impl<T> Hash for RawState<T>
where
    T: ?Sized,
{
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.state_name.hash(state);
        self.type_id.hash(state);
    }
}

// We cannot derive this because that would impose an unnecessary trait bound `T: Debug`
impl<T> Debug for RawState<T>
where
    T: ?Sized,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("RawState")
            .field("state_name", &self.state_name)
            .field("type_id", &self.type_id)
            .finish()
    }
}

/// Making [`AsState`] a sealed trait
mod private {
    use std::ops::Deref;

    use super::{BorrowedState, OwnedState};

    pub trait Sealed {}

    impl<T> Sealed for OwnedState<T> where T: ?Sized {}
    impl<T> Sealed for BorrowedState<'_, T> where T: ?Sized {}
    impl<S> Sealed for S
    where
        S: Deref,
        S::Target: Sealed,
    {
    }
}

#[cfg(test)]
mod tests {
    #![allow(dead_code)]

    use static_assertions::{assert_impl_all, assert_not_impl_any};

    use super::*;

    #[test]
    fn owned_state_is_send_and_sync_regardless_of_data_type() {
        type NeitherSendNorSync = *const ();
        assert_not_impl_any!(NeitherSendNorSync: Send, Sync);

        assert_impl_all!(OwnedState<NeitherSendNorSync>: Send, Sync);
    }

    #[test]
    fn borrowed_state_is_send_and_sync_regardless_of_data_type() {
        type NeitherSendNorSync = *const ();
        assert_not_impl_any!(NeitherSendNorSync: Send, Sync);

        assert_impl_all!(BorrowedState<'_, NeitherSendNorSync>: Send, Sync);
    }
}