| @@ -3,6 +3,7 @@ use std::{rc::Rc, sync::Arc}; · 2 unmodified lines |
| 3 | 3 | use parking_lot::lock_api::{ArcRwLockReadGuard, ArcRwLockWriteGuard}; |
| 4 | 4 | |
| 5 | 5 | use crate::{easy, Easy, EasyArc, EasyArcExclusive, EasyShared, Repository}; |
| 6 | +use std::cell::{Ref, RefMut}; |
| 6 | 7 | |
| 7 | 8 | impl Clone for Easy { |
| 8 | 9 | fn clone(&self) -> Self { |
| @@ -95,6 +96,23 @@ impl<'repo> easy::Access for EasyShared<'repo> { · 86 unmodified lines |
| 95 | 96 | } |
| 96 | 97 | } |
| 97 | 98 | |
| 99 | +impl<'repo> easy::AccessGAT for EasyShared<'repo> { |
| 100 | + type RepoRef<'a> = &'a Repository; |
| 101 | + type RepoRefMut<'a> = &'a mut Repository; |
| 102 | + |
| 103 | + fn repo(&self) -> Result<Self::RepoRef<'_>, easy::borrow::repo::Error> { |
| 104 | + Ok(self.repo) |
| 105 | + } |
| 106 | + |
| 107 | + fn repo_mut<'a>(&'a self) -> Result<Self::RepoRefMut<'repo>, easy::borrow::repo::Error> { |
| 108 | + Err(easy::borrow::repo::Error) |
| 109 | + } |
| 110 | + |
| 111 | + fn state(&self) -> &easy::State { |
| 112 | + &self.state |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 98 | 116 | impl easy::Access for Easy { |
| 99 | 117 | type RepoRef = Rc<Repository>; |
| 100 | 118 | type RepoRefMut = ArcRwLockWriteGuard<parking_lot::RawRwLock, Repository>; // this is a lie |
| @@ -112,6 +130,50 @@ impl easy::Access for Easy { · 11 unmodified lines |
| 112 | 130 | } |
| 113 | 131 | } |
| 114 | 132 | |
| 133 | +/// A handle to a `Repository` for use when the repository needs to be shared, providing state for one `ObjectRef` at a time, , created with [`Repository::into_easy()`]. |
| 134 | +/// |
| 135 | +/// For use in one-off commands that don't have to deal with the changes they potentially incur. |
| 136 | +pub struct EasyExclusive { |
| 137 | + /// The repository |
| 138 | + pub repo: Rc<std::cell::RefCell<Repository>>, |
| 139 | + /// The state with interior mutability |
| 140 | + pub state: easy::State, |
| 141 | +} |
| 142 | + |
| 143 | +impl easy::AccessGAT for EasyExclusive { |
| 144 | + type RepoRef<'a> = Ref<'a, Repository>; // this will be Ref<'a, Repository> |
| 145 | + type RepoRefMut<'a> = RefMut<'a, Repository>; // this is a lie |
| 146 | + |
| 147 | + fn repo(&self) -> Result<Self::RepoRef<'_>, easy::borrow::repo::Error> { |
| 148 | + Ok(self.repo.borrow()) |
| 149 | + } |
| 150 | + |
| 151 | + fn repo_mut(&self) -> Result<Self::RepoRefMut<'_>, easy::borrow::repo::Error> { |
| 152 | + self.repo.try_borrow_mut().map_err(Into::into) |
| 153 | + } |
| 154 | + |
| 155 | + fn state(&self) -> &easy::State { |
| 156 | + &self.state |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +impl easy::AccessGAT for EasyArcExclusive { |
| 161 | + type RepoRef<'a> = ArcRwLockReadGuard<parking_lot::RawRwLock, Repository>; |
| 162 | + type RepoRefMut<'a> = ArcRwLockWriteGuard<parking_lot::RawRwLock, Repository>; |
| 163 | + |
| 164 | + fn repo(&self) -> Result<Self::RepoRef<'_>, easy::borrow::repo::Error> { |
| 165 | + Ok(self.repo.read_arc()) |
| 166 | + } |
| 167 | + |
| 168 | + fn repo_mut(&self) -> Result<Self::RepoRefMut<'_>, easy::borrow::repo::Error> { |
| 169 | + Ok(self.repo.write_arc()) |
| 170 | + } |
| 171 | + |
| 172 | + fn state(&self) -> &easy::State { |
| 173 | + &self.state |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 115 | 177 | impl easy::Access for EasyArc { |
| 116 | 178 | type RepoRef = Arc<Repository>; |
| 117 | 179 | type RepoRefMut = ArcRwLockWriteGuard<parking_lot::RawRwLock, Repository>; // this is a lie |