macros.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // #[macro_export]
  2. // macro_rules! impl_save_func {
  3. // ($func_name:ident, $target:ident, $table_name:expr, $conn:ident) => {
  4. // fn $func_name(object: $target) -> Result<(), WorkspaceError> {
  5. // let _ = diesel::insert_into($table_name)
  6. // .values($target)
  7. // .execute(&*($conn))?;
  8. // }
  9. // };
  10. // }
  11. #[macro_export]
  12. macro_rules! impl_def_and_def_mut {
  13. ($target:ident, $item: ident) => {
  14. impl std::ops::Deref for $target {
  15. type Target = Vec<$item>;
  16. fn deref(&self) -> &Self::Target { &self.items }
  17. }
  18. impl std::ops::DerefMut for $target {
  19. fn deref_mut(&mut self) -> &mut Self::Target { &mut self.items }
  20. }
  21. impl $target {
  22. #[allow(dead_code)]
  23. pub fn into_inner(&mut self) -> Vec<$item> { ::std::mem::replace(&mut self.items, vec![]) }
  24. #[allow(dead_code)]
  25. pub fn push(&mut self, item: $item) {
  26. if self.items.contains(&item) {
  27. log::error!("add duplicate item: {:?}", item);
  28. return;
  29. }
  30. self.items.push(item);
  31. }
  32. pub fn first_or_crash(&self) -> &$item { self.items.first().unwrap() }
  33. }
  34. };
  35. }