lib.rs

  1#[cfg(any(test, feature = "test-support"))]
  2pub mod test;
  3
  4use futures::Future;
  5use std::{
  6    cmp::Ordering,
  7    ops::AddAssign,
  8    pin::Pin,
  9    task::{Context, Poll},
 10};
 11
 12pub fn truncate(s: &str, max_chars: usize) -> &str {
 13    match s.char_indices().nth(max_chars) {
 14        None => s,
 15        Some((idx, _)) => &s[..idx],
 16    }
 17}
 18
 19pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
 20    debug_assert!(max_chars >= 5);
 21
 22    if s.len() > max_chars {
 23        format!("{}", truncate(&s, max_chars.saturating_sub(3)))
 24    } else {
 25        s.to_string()
 26    }
 27}
 28
 29pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
 30    let prev = *value;
 31    *value += T::from(1);
 32    prev
 33}
 34
 35/// Extend a sorted vector with a sorted sequence of items, maintaining the vector's sort order and
 36/// enforcing a maximum length. Sort the items according to the given callback. Before calling this,
 37/// both `vec` and `new_items` should already be sorted according to the `cmp` comparator.
 38pub fn extend_sorted<T, I, F>(vec: &mut Vec<T>, new_items: I, limit: usize, mut cmp: F)
 39where
 40    I: IntoIterator<Item = T>,
 41    F: FnMut(&T, &T) -> Ordering,
 42{
 43    let mut start_index = 0;
 44    for new_item in new_items {
 45        if let Err(i) = vec[start_index..].binary_search_by(|m| cmp(m, &new_item)) {
 46            let index = start_index + i;
 47            if vec.len() < limit {
 48                vec.insert(index, new_item);
 49            } else if index < vec.len() {
 50                vec.pop();
 51                vec.insert(index, new_item);
 52            }
 53            start_index = index;
 54        }
 55    }
 56}
 57
 58pub trait ResultExt {
 59    type Ok;
 60
 61    fn log_err(self) -> Option<Self::Ok>;
 62    fn warn_on_err(self) -> Option<Self::Ok>;
 63}
 64
 65impl<T, E> ResultExt for Result<T, E>
 66where
 67    E: std::fmt::Debug,
 68{
 69    type Ok = T;
 70
 71    fn log_err(self) -> Option<T> {
 72        match self {
 73            Ok(value) => Some(value),
 74            Err(error) => {
 75                log::error!("{:?}", error);
 76                None
 77            }
 78        }
 79    }
 80
 81    fn warn_on_err(self) -> Option<T> {
 82        match self {
 83            Ok(value) => Some(value),
 84            Err(error) => {
 85                log::warn!("{:?}", error);
 86                None
 87            }
 88        }
 89    }
 90}
 91
 92pub trait TryFutureExt {
 93    fn log_err(self) -> LogErrorFuture<Self>
 94    where
 95        Self: Sized;
 96    fn warn_on_err(self) -> LogErrorFuture<Self>
 97    where
 98        Self: Sized;
 99}
100
101impl<F, T> TryFutureExt for F
102where
103    F: Future<Output = anyhow::Result<T>>,
104{
105    fn log_err(self) -> LogErrorFuture<Self>
106    where
107        Self: Sized,
108    {
109        LogErrorFuture(self, log::Level::Error)
110    }
111
112    fn warn_on_err(self) -> LogErrorFuture<Self>
113    where
114        Self: Sized,
115    {
116        LogErrorFuture(self, log::Level::Warn)
117    }
118}
119
120pub struct LogErrorFuture<F>(F, log::Level);
121
122impl<F, T> Future for LogErrorFuture<F>
123where
124    F: Future<Output = anyhow::Result<T>>,
125{
126    type Output = Option<T>;
127
128    fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
129        let level = self.1;
130        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
131        match inner.poll(cx) {
132            Poll::Ready(output) => Poll::Ready(match output {
133                Ok(output) => Some(output),
134                Err(error) => {
135                    log::log!(level, "{:?}", error);
136                    None
137                }
138            }),
139            Poll::Pending => Poll::Pending,
140        }
141    }
142}
143
144struct Defer<F: FnOnce()>(Option<F>);
145
146impl<F: FnOnce()> Drop for Defer<F> {
147    fn drop(&mut self) {
148        if let Some(f) = self.0.take() {
149            f()
150        }
151    }
152}
153
154pub fn defer<F: FnOnce()>(f: F) -> impl Drop {
155    Defer(Some(f))
156}
157
158#[cfg(test)]
159mod tests {
160    use super::*;
161
162    #[test]
163    fn test_extend_sorted() {
164        let mut vec = vec![];
165
166        extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
167        assert_eq!(vec, &[21, 17, 13, 8, 1]);
168
169        extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
170        assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
171
172        extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
173        assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
174    }
175}