lazy_weak.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::sync::{Arc, OnceLock, Weak};
  19. /// Sometimes you need a parent-child relationship which results in code like:
  20. /// ```rust
  21. /// struct Parent {
  22. /// child: Mutex<Option<Arc<Child>>>
  23. /// }
  24. /// impl Parent {
  25. /// fn new() -> Arc<Self> {
  26. /// let self_ = Arc::new(Self {
  27. /// child: Mutex::new(None)
  28. /// };
  29. /// let parent = Arc::downgrade(&self_);
  30. /// *self_.child.lock().await = Some(Child::new(parent));
  31. /// self_
  32. /// }
  33. /// }
  34. /// struct Child {
  35. /// parent: Weak<Parent>
  36. /// }
  37. /// impl Child {
  38. /// fn new(parent: Weak<Parent>) -> Self {
  39. /// Self { parent }
  40. /// }
  41. /// fn upgrade(&self) -> Arc<Parent> {
  42. /// self.parent.upgrade().unwrap()
  43. /// }
  44. /// }
  45. /// ```
  46. /// This class simplifies the above code by allowing us instead to do:
  47. /// ```rust
  48. /// struct Parent {
  49. /// child: Arc<Child>
  50. /// }
  51. /// impl Parent {
  52. /// fn new() -> Arc<Self> {
  53. /// let self_ = Arc::new(Self {
  54. /// child: Child::new()
  55. /// };
  56. /// self_.child.parent.init(self_.clone());
  57. /// self_
  58. /// }
  59. /// }
  60. /// struct Child {
  61. /// parent: LazyWeak<Parent>
  62. /// }
  63. /// impl Child {
  64. /// fn new() -> Self {
  65. /// Self { parent: LazyWeak::new() }
  66. /// }
  67. /// fn upgrade(&self) -> Arc<Parent> {
  68. /// self.parent.upgrade()
  69. /// }
  70. /// }
  71. /// ```
  72. pub struct LazyWeak<Parent>(OnceLock<Weak<Parent>>);
  73. impl<Parent> LazyWeak<Parent> {
  74. /// Create an empty `LazyWeak`, which must immediately be followed by `weak.init()`.
  75. pub fn new() -> Self {
  76. Self(OnceLock::new())
  77. }
  78. /// Must be called within the same scope as `new()`.
  79. pub fn init(&self, parent: Arc<Parent>) {
  80. assert!(self.0.get().is_none());
  81. let parent = Arc::downgrade(&parent);
  82. self.0.set(parent).unwrap();
  83. }
  84. /// Access the `Arc<Parent>` pointer
  85. pub fn upgrade(&self) -> Arc<Parent> {
  86. self.0.get().unwrap().upgrade().unwrap()
  87. }
  88. }
  89. impl<Parent> Default for LazyWeak<Parent> {
  90. fn default() -> Self {
  91. Self::new()
  92. }
  93. }