lib.rs

  1#[cfg(feature = "test-support")]
  2pub mod test;
  3
  4use futures::Future;
  5use std::{
  6    borrow::{Borrow, BorrowMut},
  7    cmp::Ordering,
  8    ops::{AddAssign, Deref, DerefMut},
  9    pin::Pin,
 10    task::{Context, Poll},
 11};
 12
 13pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
 14    let prev = *value;
 15    *value += T::from(1);
 16    prev
 17}
 18
 19/// Extend a sorted vector with a sorted sequence of items, maintaining the vector's sort order and
 20/// enforcing a maximum length. Sort the items according to the given callback. Before calling this,
 21/// both `vec` and `new_items` should already be sorted according to the `cmp` comparator.
 22pub fn extend_sorted<T, I, F>(vec: &mut Vec<T>, new_items: I, limit: usize, mut cmp: F)
 23where
 24    I: IntoIterator<Item = T>,
 25    F: FnMut(&T, &T) -> Ordering,
 26{
 27    let mut start_index = 0;
 28    for new_item in new_items {
 29        if let Err(i) = vec[start_index..].binary_search_by(|m| cmp(m, &new_item)) {
 30            let index = start_index + i;
 31            if vec.len() < limit {
 32                vec.insert(index, new_item);
 33            } else if index < vec.len() {
 34                vec.pop();
 35                vec.insert(index, new_item);
 36            }
 37            start_index = index;
 38        }
 39    }
 40}
 41pub trait ResultExt {
 42    type Ok;
 43
 44    fn log_err(self) -> Option<Self::Ok>;
 45    fn warn_on_err(self) -> Option<Self::Ok>;
 46}
 47
 48impl<T, E> ResultExt for Result<T, E>
 49where
 50    E: std::fmt::Debug,
 51{
 52    type Ok = T;
 53
 54    fn log_err(self) -> Option<T> {
 55        match self {
 56            Ok(value) => Some(value),
 57            Err(error) => {
 58                log::error!("{:?}", error);
 59                None
 60            }
 61        }
 62    }
 63
 64    fn warn_on_err(self) -> Option<T> {
 65        match self {
 66            Ok(value) => Some(value),
 67            Err(error) => {
 68                log::warn!("{:?}", error);
 69                None
 70            }
 71        }
 72    }
 73}
 74
 75pub trait TryFutureExt {
 76    fn log_err(self) -> LogErrorFuture<Self>
 77    where
 78        Self: Sized;
 79    fn warn_on_err(self) -> LogErrorFuture<Self>
 80    where
 81        Self: Sized;
 82}
 83
 84impl<F, T> TryFutureExt for F
 85where
 86    F: Future<Output = anyhow::Result<T>>,
 87{
 88    fn log_err(self) -> LogErrorFuture<Self>
 89    where
 90        Self: Sized,
 91    {
 92        LogErrorFuture(self, log::Level::Error)
 93    }
 94
 95    fn warn_on_err(self) -> LogErrorFuture<Self>
 96    where
 97        Self: Sized,
 98    {
 99        LogErrorFuture(self, log::Level::Warn)
100    }
101}
102
103pub struct LogErrorFuture<F>(F, log::Level);
104
105impl<F, T> Future for LogErrorFuture<F>
106where
107    F: Future<Output = anyhow::Result<T>>,
108{
109    type Output = Option<T>;
110
111    fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
112        let level = self.1;
113        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
114        match inner.poll(cx) {
115            Poll::Ready(output) => Poll::Ready(match output {
116                Ok(output) => Some(output),
117                Err(error) => {
118                    log::log!(level, "{:?}", error);
119                    None
120                }
121            }),
122            Poll::Pending => Poll::Pending,
123        }
124    }
125}
126
127pub enum CowMut<'a, T: ?Sized + ToOwned> {
128    Borrowed(&'a mut T),
129    Owned(T::Owned),
130}
131
132impl<'a, T> Deref for CowMut<'a, T>
133where
134    T: ?Sized + ToOwned,
135{
136    type Target = T;
137
138    fn deref(&self) -> &Self::Target {
139        match self {
140            CowMut::Borrowed(value) => value,
141            CowMut::Owned(value) => value.borrow(),
142        }
143    }
144}
145
146impl<'a, T> DerefMut for CowMut<'a, T>
147where
148    T: ?Sized + ToOwned,
149    T::Owned: BorrowMut<T>,
150{
151    fn deref_mut(&mut self) -> &mut Self::Target {
152        match self {
153            CowMut::Borrowed(value) => value,
154            CowMut::Owned(value) => value.borrow_mut(),
155        }
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn test_extend_sorted() {
165        let mut vec = vec![];
166
167        extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
168        assert_eq!(vec, &[21, 17, 13, 8, 1]);
169
170        extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
171        assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
172
173        extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
174        assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
175    }
176}
177
178// Allow surf Results to accept context like other Results do when
179// using anyhow.
180pub trait SurfResultExt {
181    fn context<C>(self, cx: C) -> Self
182    where
183        C: std::fmt::Display + Send + Sync + 'static;
184
185    fn with_context<C, F>(self, f: F) -> Self
186    where
187        C: std::fmt::Display + Send + Sync + 'static,
188        F: FnOnce() -> C;
189}
190
191impl<T> SurfResultExt for surf::Result<T> {
192    fn context<C>(self, cx: C) -> Self
193    where
194        C: std::fmt::Display + Send + Sync + 'static,
195    {
196        self.map_err(|e| surf::Error::new(e.status(), e.into_inner().context(cx)))
197    }
198
199    fn with_context<C, F>(self, f: F) -> Self
200    where
201        C: std::fmt::Display + Send + Sync + 'static,
202        F: FnOnce() -> C,
203    {
204        self.map_err(|e| surf::Error::new(e.status(), e.into_inner().context(f())))
205    }
206}