History

[repository #165] PROOF: GATs will work as expected!

When GATs land, RefCell will also be supported, and even EasyOwned which is the Rc-free version of Easy.

Sebastian Thiel committed 5 years ago

Commit 7f56dbd1 parent 1547d0b3 files · +76 −0Browse files

Unified · Split

3 files
  1. git-repository/src/easy/impls.rs +62 −0
  2. git-repository/src/easy/mod.rs +13 −0
  3. git-repository/src/lib.rs +1 −0
git-repository/src/easy/impls.rs+62−0
@@ -3,6 +3,7 @@ use std::{rc::Rc, sync::Arc}; · 2 unmodified lines
3use parking_lot::lock_api::{ArcRwLockReadGuard, ArcRwLockWriteGuard};3use parking_lot::lock_api::{ArcRwLockReadGuard, ArcRwLockWriteGuard};
4 4
5use crate::{easy, Easy, EasyArc, EasyArcExclusive, EasyShared, Repository};5use crate::{easy, Easy, EasyArc, EasyArcExclusive, EasyShared, Repository};
6use std::cell::{Ref, RefMut};
6 7
7impl Clone for Easy {8impl Clone for Easy {
8 fn clone(&self) -> Self {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
99impl<'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
98impl easy::Access for Easy {116impl easy::Access for Easy {
99 type RepoRef = Rc<Repository>;117 type RepoRef = Rc<Repository>;
100 type RepoRefMut = ArcRwLockWriteGuard<parking_lot::RawRwLock, Repository>; // this is a lie118 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.
136pub 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
143impl 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
160impl 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
115impl easy::Access for EasyArc {177impl easy::Access for EasyArc {
116 type RepoRef = Arc<Repository>;178 type RepoRef = Arc<Repository>;
117 type RepoRefMut = ArcRwLockWriteGuard<parking_lot::RawRwLock, Repository>; // this is a lie179 type RepoRefMut = ArcRwLockWriteGuard<parking_lot::RawRwLock, Repository>; // this is a lie
git-repository/src/easy/mod.rs+13−0
@@ -85,4 +85,17 @@ pub trait Access { · 84 unmodified lines
85 fn state(&self) -> &State;85 fn state(&self) -> &State;
86}86}
87 87
88pub trait AccessGAT {
89 type RepoRef<'a>: Deref<Target = Repository> + 'a;
90 // TODO: Once GATs become stable, try to use them to make it work with RefCells too, aka EasyExclusive
91 type RepoRefMut<'a>: DerefMut<Target = Repository> + 'a;
92
93 fn repo<'a>(&self) -> std::result::Result<Self::RepoRef<'_>, borrow::repo::Error>;
94 /// # NOTE
95 ///
96 /// This is implemented only for `EasyArcExclusive` to be obtained via `to_easy_arc_exclusive()`
97 fn repo_mut<'a>(&'a self) -> std::result::Result<Self::RepoRefMut<'a>, borrow::repo::Error>;
98 fn state(&self) -> &State;
99}
100
88pub type Result<T> = std::result::Result<T, borrow::state::Error>;101pub type Result<T> = std::result::Result<T, borrow::state::Error>;
git-repository/src/lib.rs+1−0
@@ -1,3 +1,4 @@
1#![feature(generic_associated_types)]
1//! This crate provides the [`Repository`] abstraction which serves as a hub into all the functionality of git.2//! This crate provides the [`Repository`] abstraction which serves as a hub into all the functionality of git.
2//!3//!
3//! It's powerful and won't sacrifice performance while still increasing convenience compared to using the sub-crates4//! It's powerful and won't sacrifice performance while still increasing convenience compared to using the sub-crates

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t