async-winit 0.2.1

Use winit like an async runtime
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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*

`async-winit` is free software: you can redistribute it and/or modify it under the terms of one of
the following licenses:

* GNU Lesser General Public License as published by the Free Software Foundation, either
  version 3 of the License, or (at your option) any later version.
* Mozilla Public License as published by the Mozilla Foundation, version 2.

`async-winit` is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
Public License and the Patron License for more details.

You should have received a copy of the GNU Lesser General Public License and the Mozilla
Public License along with `async-winit`. If not, see <https://www.gnu.org/licenses/>.

*/

use crate::reactor::Reactor;
pub(crate) use __private::__ThreadSafety;

use core::cell::{Cell, RefCell, RefMut};
use core::convert::Infallible;
use core::future::Future;
use core::ops::Add;

use std::collections::VecDeque;
use std::rc::Rc;
use std::sync::atomic;
use std::thread;

use unsend::channel as us_channel;

pub(crate) mod prelude {
    pub use super::__private::{Atomic, Mutex, OnceLock};
}

#[cfg(feature = "thread_safe")]
pub use thread_safe::ThreadSafe;

#[cfg(feature = "thread_safe")]
type _DefaultTS = ThreadSafe;
#[cfg(not(feature = "thread_safe"))]
type _DefaultTS = ThreadUnsafe;

/// The default thread safe type to use.
pub type DefaultThreadSafety = _DefaultTS;

/// A token that can be used to indicate whether the current implementation should be thread-safe or
/// not.
pub trait ThreadSafety: __ThreadSafety {}

/// Use thread-unsafe primitives.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ThreadUnsafe {
    _private: (),
}

impl ThreadSafety for ThreadUnsafe {}

impl __ThreadSafety for ThreadUnsafe {
    type Error = Infallible;

    type AtomicUsize = Cell<usize>;
    type AtomicU64 = Cell<u64>;
    type AtomicI64 = Cell<i64>;

    type Receiver<T> = us_channel::Receiver<T>;
    type Sender<T> = us_channel::Sender<T>;
    type Rc<T> = Rc<T>;

    type ConcurrentQueue<T> = RefCell<VecDeque<T>>;
    type Mutex<T> = RefCell<T>;
    type OnceLock<T> = once_cell::unsync::OnceCell<T>;

    fn channel_bounded<T>(_capacity: usize) -> (Self::Sender<T>, Self::Receiver<T>) {
        us_channel::channel()
    }

    fn get_reactor() -> Self::Rc<Reactor<Self>> {
        use once_cell::sync::OnceCell;

        /// The thread ID of the thread that created the reactor.
        static REACTOR_THREAD_ID: OnceCell<thread::ThreadId> = OnceCell::new();

        std::thread_local! {
            static REACTOR: RefCell<Option<std::rc::Rc<Reactor<ThreadUnsafe>>>> = RefCell::new(None);
        }

        // Try to set the thread ID.
        let thread_id = thread_id();
        let reactor_thread_id = REACTOR_THREAD_ID.get_or_init(|| thread_id);

        if thread_id != *reactor_thread_id {
            panic!("The reactor must be created on the main thread");
        }

        REACTOR.with(|reactor| {
            reactor
                .borrow_mut()
                .get_or_insert_with(|| std::rc::Rc::new(Reactor::new()))
                .clone()
        })
    }
}

pub(crate) type MutexGuard<'a, T, TS> =
    <<TS as __ThreadSafety>::Mutex<T> as __private::Mutex<T>>::Lock<'a>;

fn thread_id() -> thread::ThreadId {
    // Get the address of a thread-local variable.
    std::thread_local! {
        static THREAD_ID: Cell<Option<thread::ThreadId>> = Cell::new(None);
    }

    THREAD_ID
        .try_with(|thread_id| {
            thread_id.get().unwrap_or_else(|| {
                let id = thread::current().id();
                thread_id.set(Some(id));
                id
            })
        })
        .unwrap_or_else(|_| {
            // We're in a destructor
            thread::current().id()
        })
}

impl<T: Copy> __private::Atomic<T> for Cell<T> {
    fn new(value: T) -> Self {
        Self::new(value)
    }

    fn load(&self, _order: atomic::Ordering) -> T {
        self.get()
    }

    fn store(&self, value: T, _order: atomic::Ordering) {
        self.set(value);
    }

    fn fetch_add(&self, value: T, _order: atomic::Ordering) -> T
    where
        T: Add<Output = T>,
    {
        let old = self.get();
        self.set(old + value);
        old
    }
}

impl<T> __private::Sender<T> for us_channel::Sender<T> {
    type Error = Infallible;
    type Send<'a> = core::future::Ready<Result<(), Self::Error>> where Self: 'a;

    fn send(&self, value: T) -> Self::Send<'_> {
        self.send(value).ok();
        core::future::ready(Ok(()))
    }

    fn try_send(&self, value: T) -> Result<(), Self::Error> {
        self.send(value).ok();
        Ok(())
    }
}

impl<T> __private::Receiver<T> for us_channel::Receiver<T> {
    type Error = ();
    type Recv<'a> = std::pin::Pin<Box<dyn Future<Output = Result<T, Self::Error>> + 'a>> where Self: 'a;

    fn recv(&self) -> Self::Recv<'_> {
        Box::pin(async move { self.recv().await.map_err(|_| ()) })
    }

    fn capacity(&self) -> usize {
        usize::MAX
    }

    fn try_recv(&self) -> Option<T> {
        self.try_recv().ok()
    }

    fn len(&self) -> usize {
        todo!()
    }
}

impl<T> __private::ConcurrentQueue<T> for RefCell<VecDeque<T>> {
    type TryIter<'a> = TryIter<'a, T> where Self: 'a;

    fn bounded(capacity: usize) -> Self {
        Self::new(VecDeque::with_capacity(capacity))
    }

    fn push(&self, value: T) -> Result<(), T> {
        self.borrow_mut().push_back(value);
        Ok(())
    }

    fn pop(&self) -> Option<T> {
        self.borrow_mut().pop_front()
    }

    fn capacity(&self) -> usize {
        usize::MAX
    }

    fn try_iter(&self) -> Self::TryIter<'_> {
        TryIter { queue: self }
    }
}

#[doc(hidden)]
pub struct TryIter<'a, T> {
    queue: &'a RefCell<VecDeque<T>>,
}

impl<'a, T> Iterator for TryIter<'a, T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.queue.borrow_mut().pop_front()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.queue.borrow().len();
        (len, Some(len))
    }
}

impl<T> __private::Mutex<T> for RefCell<T> {
    type Error = Infallible;
    type Lock<'a> = RefMut<'a, T> where Self: 'a;

    fn new(value: T) -> Self {
        Self::new(value)
    }

    fn lock(&self) -> Result<Self::Lock<'_>, Self::Error> {
        Ok(self.borrow_mut())
    }
}

impl<T> __private::OnceLock<T> for once_cell::unsync::OnceCell<T> {
    fn new() -> Self {
        Self::new()
    }

    fn get(&self) -> Option<&T> {
        self.get()
    }

    fn set(&self, value: T) -> Result<(), T> {
        self.set(value)
    }

    fn get_or_init<F>(&self, f: F) -> &T
    where
        F: FnOnce() -> T,
    {
        self.get_or_init(f)
    }
}

impl<T> __private::Rc<T> for std::rc::Rc<T> {
    fn new(value: T) -> Self {
        Self::new(value)
    }
}

#[cfg(feature = "thread_safe")]
pub(crate) mod thread_safe {
    use super::*;

    use concurrent_queue::ConcurrentQueue;
    use std::sync::atomic;
    use std::sync::{Arc, Mutex};

    /// Use thread-safe primitives.
    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct ThreadSafe {
        _private: (),
    }

    impl ThreadSafety for ThreadSafe {}

    impl __ThreadSafety for ThreadSafe {
        type Error = Box<dyn std::error::Error + Send + Sync>;

        type AtomicI64 = atomic::AtomicI64;
        type AtomicUsize = atomic::AtomicUsize;
        type AtomicU64 = atomic::AtomicU64;

        type Sender<T> = async_channel::Sender<T>;
        type Receiver<T> = async_channel::Receiver<T>;

        type ConcurrentQueue<T> = ConcurrentQueue<T>;
        type Mutex<T> = Mutex<T>;
        type OnceLock<T> = once_cell::sync::OnceCell<T>;
        type Rc<T> = Arc<T>;

        fn channel_bounded<T>(capacity: usize) -> (Self::Sender<T>, Self::Receiver<T>) {
            async_channel::bounded(capacity)
        }
        fn get_reactor() -> Self::Rc<crate::reactor::Reactor<Self>>
        where
            Self: super::ThreadSafety,
        {
            use once_cell::sync::OnceCell;

            static REACTOR: OnceCell<Arc<Reactor<ThreadSafe>>> = OnceCell::new();

            REACTOR.get_or_init(|| Arc::new(Reactor::new())).clone()
        }
    }

    impl __private::Atomic<i64> for atomic::AtomicI64 {
        fn new(value: i64) -> Self {
            Self::new(value)
        }

        fn fetch_add(&self, value: i64, order: atomic::Ordering) -> i64 {
            self.fetch_add(value, order)
        }

        fn load(&self, order: atomic::Ordering) -> i64 {
            self.load(order)
        }

        fn store(&self, value: i64, order: atomic::Ordering) {
            self.store(value, order)
        }
    }

    impl __private::Atomic<usize> for atomic::AtomicUsize {
        fn new(value: usize) -> Self {
            Self::new(value)
        }

        fn fetch_add(&self, value: usize, order: atomic::Ordering) -> usize {
            self.fetch_add(value, order)
        }

        fn load(&self, order: atomic::Ordering) -> usize {
            self.load(order)
        }

        fn store(&self, value: usize, order: atomic::Ordering) {
            self.store(value, order)
        }
    }

    impl __private::Atomic<u64> for atomic::AtomicU64 {
        fn new(value: u64) -> Self {
            Self::new(value)
        }

        fn fetch_add(&self, value: u64, order: atomic::Ordering) -> u64 {
            self.fetch_add(value, order)
        }

        fn load(&self, order: atomic::Ordering) -> u64 {
            self.load(order)
        }

        fn store(&self, value: u64, order: atomic::Ordering) {
            self.store(value, order)
        }
    }

    impl<T> __private::Sender<T> for async_channel::Sender<T> {
        type Error = async_channel::SendError<T>;
        type Send<'a> = async_channel::Send<'a, T> where Self: 'a;

        fn send(&self, value: T) -> Self::Send<'_> {
            self.send(value)
        }

        fn try_send(&self, value: T) -> Result<(), Self::Error> {
            self.try_send(value).map_err(|_e| todo!())
        }
    }

    impl<T> __private::Receiver<T> for async_channel::Receiver<T> {
        type Error = async_channel::RecvError;
        type Recv<'a> = async_channel::Recv<'a, T> where Self: 'a;

        fn recv(&self) -> Self::Recv<'_> {
            self.recv()
        }

        fn capacity(&self) -> usize {
            self.capacity().unwrap()
        }

        fn try_recv(&self) -> Option<T> {
            self.try_recv().ok()
        }

        fn len(&self) -> usize {
            self.len()
        }
    }

    impl<T> __private::ConcurrentQueue<T> for ConcurrentQueue<T> {
        type TryIter<'a> = concurrent_queue::TryIter<'a, T> where Self: 'a;

        fn bounded(capacity: usize) -> Self {
            Self::bounded(capacity)
        }

        fn push(&self, value: T) -> Result<(), T> {
            self.push(value).map_err(|e| e.into_inner())
        }

        fn pop(&self) -> Option<T> {
            self.pop().ok()
        }

        fn capacity(&self) -> usize {
            self.capacity().unwrap()
        }

        fn try_iter(&self) -> Self::TryIter<'_> {
            self.try_iter()
        }
    }

    impl<T> __private::Mutex<T> for Mutex<T> {
        type Error = Infallible;
        type Lock<'a> = std::sync::MutexGuard<'a, T> where Self: 'a;

        fn new(value: T) -> Self {
            Self::new(value)
        }

        fn lock(&self) -> Result<Self::Lock<'_>, Self::Error> {
            Ok(self.lock().unwrap_or_else(|e| e.into_inner()))
        }
    }

    impl<T> __private::OnceLock<T> for once_cell::sync::OnceCell<T> {
        fn new() -> Self {
            Self::new()
        }

        fn get(&self) -> Option<&T> {
            self.get()
        }

        fn set(&self, value: T) -> Result<(), T> {
            self.set(value)
        }

        fn get_or_init<F>(&self, f: F) -> &T
        where
            F: FnOnce() -> T,
        {
            self.get_or_init(f)
        }
    }

    impl<T> __private::Rc<T> for Arc<T> {
        fn new(value: T) -> Self {
            Self::new(value)
        }
    }
}

pub(crate) mod __private {
    use core::fmt::{Debug, Display};
    use core::future::Future;
    use core::ops::{Add, Deref, DerefMut};
    use core::sync::atomic;

    #[doc(hidden)]
    pub trait __ThreadSafety: Sized {
        type Error: Display + Debug;

        type AtomicI64: Atomic<i64>;
        type AtomicUsize: Atomic<usize>;
        type AtomicU64: Atomic<u64>;

        type Sender<T>: Sender<T>;
        type Receiver<T>: Receiver<T>;

        type ConcurrentQueue<T>: ConcurrentQueue<T>;
        type Mutex<T>: Mutex<T>;
        type OnceLock<T>: OnceLock<T>;
        type Rc<T>: Rc<T>;

        fn channel_bounded<T>(capacity: usize) -> (Self::Sender<T>, Self::Receiver<T>);
        fn get_reactor() -> Self::Rc<crate::reactor::Reactor<Self>>
        where
            Self: super::ThreadSafety;
    }

    #[doc(hidden)]
    pub trait Atomic<T> {
        fn new(value: T) -> Self;
        fn load(&self, order: atomic::Ordering) -> T;
        fn store(&self, value: T, order: atomic::Ordering);
        fn fetch_add(&self, value: T, order: atomic::Ordering) -> T
        where
            T: Add<Output = T>;
    }

    #[doc(hidden)]
    pub trait Sender<T> {
        type Error;
        type Send<'a>: Future<Output = Result<(), Self::Error>> + 'a
        where
            Self: 'a;
        fn send(&self, value: T) -> Self::Send<'_>;
        fn try_send(&self, value: T) -> Result<(), Self::Error>;
    }

    #[doc(hidden)]
    pub trait Receiver<T> {
        type Error: std::fmt::Debug;
        type Recv<'a>: Future<Output = Result<T, Self::Error>> + 'a
        where
            Self: 'a;

        fn recv(&self) -> Self::Recv<'_>;
        fn capacity(&self) -> usize;
        fn try_recv(&self) -> Option<T>;
        fn len(&self) -> usize;
    }

    #[doc(hidden)]
    pub trait OnceLock<T> {
        fn new() -> Self;
        fn get(&self) -> Option<&T>;
        fn get_or_init<F>(&self, f: F) -> &T
        where
            F: FnOnce() -> T;
        fn set(&self, value: T) -> Result<(), T>;
    }

    #[doc(hidden)]
    pub trait Mutex<T> {
        type Error: Debug + Display;
        type Lock<'a>: DerefMut<Target = T> + 'a
        where
            Self: 'a;

        fn new(value: T) -> Self;
        fn lock(&self) -> Result<Self::Lock<'_>, Self::Error>;
    }

    #[doc(hidden)]
    pub trait ConcurrentQueue<T> {
        type TryIter<'a>: Iterator<Item = T> + 'a
        where
            Self: 'a;

        fn bounded(capacity: usize) -> Self;
        fn push(&self, value: T) -> Result<(), T>;
        fn pop(&self) -> Option<T>;
        fn capacity(&self) -> usize;
        fn try_iter(&self) -> Self::TryIter<'_>;
    }

    #[doc(hidden)]
    pub trait Rc<T>: Clone + Deref<Target = T> {
        fn new(value: T) -> Self;
    }
}