1use crate::{BackgroundExecutor, Task};
2use std::{
3 future::Future,
4 pin::Pin,
5 sync::atomic::{AtomicUsize, Ordering::SeqCst},
6 task,
7 time::Duration,
8};
9
10pub trait FluentBuilder {
12 fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
14 where
15 Self: Sized,
16 {
17 f(self)
18 }
19
20 fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
22 where
23 Self: Sized,
24 {
25 self.map(|this| if condition { then(this) } else { this })
26 }
27
28 fn when_else(
30 self,
31 condition: bool,
32 then: impl FnOnce(Self) -> Self,
33 else_fn: impl FnOnce(Self) -> Self,
34 ) -> Self
35 where
36 Self: Sized,
37 {
38 self.map(|this| if condition { then(this) } else { else_fn(this) })
39 }
40
41 fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
43 where
44 Self: Sized,
45 {
46 self.map(|this| {
47 if let Some(value) = option {
48 then(this, value)
49 } else {
50 this
51 }
52 })
53 }
54 fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self
56 where
57 Self: Sized,
58 {
59 self.map(|this| if option.is_some() { this } else { then(this) })
60 }
61}
62
63pub trait FutureExt {
65 fn with_timeout(self, timeout: Duration, executor: &BackgroundExecutor) -> WithTimeout<Self>
68 where
69 Self: Sized;
70}
71
72impl<T: Future> FutureExt for T {
73 fn with_timeout(self, timeout: Duration, executor: &BackgroundExecutor) -> WithTimeout<Self>
74 where
75 Self: Sized,
76 {
77 WithTimeout {
78 future: self,
79 timer: executor.timer(timeout),
80 }
81 }
82}
83
84#[pin_project::pin_project]
85pub struct WithTimeout<T> {
86 #[pin]
87 future: T,
88 #[pin]
89 timer: Task<()>,
90}
91
92#[derive(Debug, thiserror::Error)]
93#[error("Timed out before future resolved")]
94pub struct Timeout;
96
97impl<T: Future> Future for WithTimeout<T> {
98 type Output = Result<T::Output, Timeout>;
99
100 fn poll(self: Pin<&mut Self>, cx: &mut task::Context) -> task::Poll<Self::Output> {
101 let this = self.project();
102
103 if let task::Poll::Ready(output) = this.future.poll(cx) {
104 task::Poll::Ready(Ok(output))
105 } else if this.timer.poll(cx).is_ready() {
106 task::Poll::Ready(Err(Timeout))
107 } else {
108 task::Poll::Pending
109 }
110 }
111}
112
113pub(crate) fn atomic_incr_if_not_zero(counter: &AtomicUsize) -> usize {
116 let mut loaded = counter.load(SeqCst);
117 loop {
118 if loaded == 0 {
119 return 0;
120 }
121 match counter.compare_exchange_weak(loaded, loaded + 1, SeqCst, SeqCst) {
122 Ok(x) => return x + 1,
123 Err(actual) => loaded = actual,
124 }
125 }
126}
127
128#[inline]
130pub(crate) fn round_half_toward_zero(value: f32) -> f32 {
131 (value.abs() - 0.5).ceil().copysign(value)
132}
133
134#[inline]
135pub(crate) fn round_half_toward_zero_f64(value: f64) -> f64 {
136 (value.abs() - 0.5).ceil().copysign(value)
137}
138
139#[inline]
140pub(crate) fn round_to_device_pixel(logical: f32, scale_factor: f32) -> f32 {
141 round_half_toward_zero(logical * scale_factor)
142}
143
144#[inline]
145pub(crate) fn round_stroke_to_device_pixel(logical: f32, scale_factor: f32) -> f32 {
146 if logical == 0.0 {
147 0.0
148 } else {
149 round_to_device_pixel(logical.max(0.0), scale_factor).max(1.0)
150 }
151}
152
153#[inline]
154pub(crate) fn floor_to_device_pixel(logical: f32, scale_factor: f32) -> f32 {
155 (logical * scale_factor).floor()
156}
157
158#[inline]
159pub(crate) fn ceil_to_device_pixel(logical: f32, scale_factor: f32) -> f32 {
160 (logical * scale_factor).ceil()
161}
162
163#[cfg(test)]
164mod tests {
165 use crate::TestAppContext;
166
167 use super::*;
168
169 #[test]
170 fn test_round_half_toward_zero() {
171 assert_eq!(round_half_toward_zero(0.5), 0.0);
173 assert_eq!(round_half_toward_zero(1.5), 1.0);
174 assert_eq!(round_half_toward_zero(2.5), 2.0);
175 assert_eq!(round_half_toward_zero(-0.5), 0.0);
176 assert_eq!(round_half_toward_zero(-1.5), -1.0);
177 assert_eq!(round_half_toward_zero(-2.5), -2.0);
178
179 assert_eq!(round_half_toward_zero(1.5001), 2.0);
181 assert_eq!(round_half_toward_zero(1.4999), 1.0);
182 assert_eq!(round_half_toward_zero(-1.5001), -2.0);
183 assert_eq!(round_half_toward_zero(-1.4999), -1.0);
184
185 assert_eq!(round_half_toward_zero(0.0), 0.0);
187 assert_eq!(round_half_toward_zero(3.0), 3.0);
188 assert_eq!(round_half_toward_zero(-3.0), -3.0);
189 }
190
191 #[test]
192 fn test_device_pixel_helpers() {
193 assert_eq!(round_to_device_pixel(1.0, 1.5), 1.0);
195 assert_eq!(round_to_device_pixel(0.3, 2.0), 1.0);
197 assert_eq!(round_to_device_pixel(1.4, 1.0), 1.0);
198 assert_eq!(round_to_device_pixel(1.6, 1.0), 2.0);
199
200 assert_eq!(round_stroke_to_device_pixel(0.0, 1.0), 0.0);
202 assert_eq!(round_stroke_to_device_pixel(0.4, 1.0), 1.0);
203 assert_eq!(round_stroke_to_device_pixel(0.5, 1.0), 1.0);
204 assert_eq!(round_stroke_to_device_pixel(1.0, 1.5), 1.0);
205 assert_eq!(round_stroke_to_device_pixel(1.6, 1.0), 2.0);
206
207 assert_eq!(floor_to_device_pixel(0.3, 2.0), 0.0);
209 assert_eq!(ceil_to_device_pixel(0.3, 2.0), 1.0);
210 assert_eq!(floor_to_device_pixel(2.1, 1.0), 2.0);
211 assert_eq!(ceil_to_device_pixel(2.1, 1.0), 3.0);
212
213 assert_eq!(round_to_device_pixel(2.0, 2.0), 4.0);
215 assert_eq!(floor_to_device_pixel(2.0, 2.0), 4.0);
216 assert_eq!(ceil_to_device_pixel(2.0, 2.0), 4.0);
217 }
218
219 #[test]
220 fn test_round_half_toward_zero_f64() {
221 assert_eq!(round_half_toward_zero_f64(0.5), 0.0);
222 assert_eq!(round_half_toward_zero_f64(-0.5), 0.0);
223 assert_eq!(round_half_toward_zero_f64(1.5), 1.0);
224 assert_eq!(round_half_toward_zero_f64(-1.5), -1.0);
225 assert_eq!(round_half_toward_zero_f64(2.5001), 3.0);
226 }
227
228 #[gpui::test]
229 async fn test_with_timeout(cx: &mut TestAppContext) {
230 Task::ready(())
231 .with_timeout(Duration::from_secs(1), &cx.executor())
232 .await
233 .expect("Timeout should be noop");
234
235 let long_duration = Duration::from_secs(6000);
236 let short_duration = Duration::from_secs(1);
237 cx.executor()
238 .timer(long_duration)
239 .with_timeout(short_duration, &cx.executor())
240 .await
241 .expect_err("timeout should have triggered");
242
243 let fut = cx
244 .executor()
245 .timer(long_duration)
246 .with_timeout(short_duration, &cx.executor());
247 cx.executor().advance_clock(short_duration * 2);
248 futures::FutureExt::now_or_never(fut)
249 .unwrap_or_else(|| panic!("timeout should have triggered"))
250 .expect_err("timeout");
251 }
252}