util.rs 796 B

1234567891011121314151617181920212223242526272829303132
  1. pub fn move_vec_element<T, F>(
  2. vec: &mut Vec<T>,
  3. filter: F,
  4. _from_index: usize,
  5. mut to_index: usize,
  6. ) -> Result<bool, String>
  7. where
  8. F: FnMut(&T) -> bool,
  9. {
  10. match vec.iter().position(filter) {
  11. None => Ok(false),
  12. Some(index) => {
  13. if vec.len() > to_index {
  14. let removed_element = vec.remove(index);
  15. vec.insert(to_index, removed_element);
  16. Ok(true)
  17. } else {
  18. let msg = format!(
  19. "Move element to invalid index: {}, current len: {}",
  20. to_index,
  21. vec.len()
  22. );
  23. Err(msg)
  24. }
  25. }
  26. }
  27. }
  28. #[allow(dead_code)]
  29. pub fn timestamp() -> i64 {
  30. chrono::Utc::now().timestamp()
  31. }