1use crate::{
2 mutex::FakeRawMutex,
3 notifier_impls::*,
4 prelude::*,
5 timeout_trait::{delay::*, fake_impls::*},
6};
7cfg_if::cfg_if! {
8 if #[cfg(feature = "std")] {
9 use std::thread;
10 use crate::timeout_trait::std_impls::*;
11 }
12}
13
14pub struct StdOs;
18#[cfg(feature = "std")]
19impl OsInterface for StdOs {
20 type RawMutex = FakeRawMutex;
21 type Notifier = StdNotifier;
22 type NotifyWaiter = StdNotifyWaiter;
23 type Instant = StdTickInstant;
24 type Delay = StdDelayNs;
25
26 const O: Self = Self {};
27
28 #[inline]
29 fn yield_thread() {
30 thread::yield_now();
31 }
32
33 #[inline]
34 fn delay() -> Self::Delay {
35 StdDelayNs {}
36 }
37
38 #[inline]
39 fn notify() -> (Self::Notifier, Self::NotifyWaiter) {
40 StdNotifier::new()
41 }
42}
43
44pub struct FakeOs;
47impl OsInterface for FakeOs {
48 type RawMutex = FakeRawMutex;
49 type Notifier = FakeNotifier;
50 type NotifyWaiter = FakeNotifier;
51 type Instant = FakeTickInstant;
52 type Delay = TickDelay<FakeTickInstant>;
53
54 const O: Self = Self {};
55
56 #[inline]
57 fn yield_thread() {}
58
59 #[inline]
60 fn delay() -> Self::Delay {
61 TickDelay::<FakeTickInstant>::default()
62 }
63
64 #[inline]
65 fn notify() -> (Self::Notifier, Self::NotifyWaiter) {
66 FakeNotifier::new()
67 }
68}
69
70#[cfg(feature = "std")]
73#[cfg(test)]
74mod tests {
75 use super::*;
76 use crate::{Duration, Mutex, Timeout};
77
78 struct OsUser<OS: OsInterface> {
79 notifier: OS::Notifier,
80 waiter: OS::NotifyWaiter,
81 mutex: Mutex<OS, u8>,
82 interval: Timeout<OS>,
83 }
84
85 impl<OS: OsInterface> OsUser<OS> {
86 fn new() -> Self {
87 let (notifier, waiter) = OS::notify();
88 Self {
89 notifier,
90 waiter,
91 mutex: OS::mutex(1),
92 interval: Timeout::<OS>::from_millis(1),
93 }
94 }
95
96 fn use_os(&mut self) {
97 let mutex = Mutex::<OS, _>::new(0);
98
99 let mut guard = mutex.try_lock().unwrap();
100 assert_eq!(*guard, 0);
101 *guard = 4;
102 drop(guard);
103
104 mutex
105 .try_with_lock(|data| {
106 assert_eq!(*data, 4);
107 *data = 5;
108 })
109 .unwrap();
110
111 OS::yield_thread();
112 OS::delay().delay_ms(1);
113
114 assert!(self.notifier.notify());
115 assert!(self.waiter.wait(&Duration::<OS>::from_millis(1)));
116
117 let mut d = self.mutex.lock();
118 *d = 2;
119
120 if self.interval.timeout() {}
121 }
122 }
123
124 #[test]
125 fn select_os() {
126 let mut user = OsUser::<FakeOs>::new();
127 user.use_os();
128 let mut user = OsUser::<StdOs>::new();
129 user.use_os();
130 }
131}
132
133#[allow(dead_code)]
134#[cfg(feature = "std")]
135#[cfg(test)]
136mod tests_end_type {
137 use crate::{StdOs as OS, os_type_alias, prelude::*};
138
139 os_type_alias!(OS);
140
141 struct EndUser {
142 notifier: Notifier,
143 waiter: NotifyWaiter,
144 mutex: Mutex<u8>,
145 interval: Timeout,
146 dur: Duration,
147 }
148
149 impl EndUser {
150 pub fn new() -> Self {
151 let (notifier, waiter) = OS::notify();
152 Self {
153 notifier,
154 waiter,
155 mutex: Mutex::new(1),
156 interval: Timeout::from_millis(1),
157 dur: Duration::from_millis(1),
158 }
159 }
160 }
161}