macros.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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<(), FlowyError> {
  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 {
  17. &self.items
  18. }
  19. }
  20. impl std::ops::DerefMut for $target {
  21. fn deref_mut(&mut self) -> &mut Self::Target {
  22. &mut self.items
  23. }
  24. }
  25. impl $target {
  26. #[allow(dead_code)]
  27. pub fn into_inner(&mut self) -> Vec<$item> {
  28. ::std::mem::take(&mut self.items)
  29. }
  30. #[allow(dead_code)]
  31. pub fn push(&mut self, item: $item) {
  32. if self.items.contains(&item) {
  33. log::error!("add duplicate item: {:?}", item);
  34. return;
  35. }
  36. self.items.push(item);
  37. }
  38. pub fn first_or_crash(&self) -> &$item {
  39. self.items.first().unwrap()
  40. }
  41. }
  42. };
  43. }