Skip to content

Commit b64029a

Browse files
committed
Remove lifetime anchors in from_raw functions
These ended up not pulling their weight and the types weren't always available. They're all unsafe already so they're sadly just a little more unsafe now, but probably not called from a public setting. Also renames a lot of lifetimes to be more specific as to what they're referencing.
1 parent 5920cb1 commit b64029a

19 files changed

+192
-212
lines changed

src/blob.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@ use std::kinds::marker;
22
use std::mem;
33
use std::raw as stdraw;
44

5-
use {raw, Oid, Repository};
5+
use {raw, Oid};
66

77
/// A structure to represent a git [blob][1]
88
///
99
/// [1]: http://git-scm.com/book/en/Git-Internals-Git-Objects
10-
pub struct Blob<'a> {
10+
pub struct Blob<'repo> {
1111
raw: *mut raw::git_blob,
12-
marker1: marker::ContravariantLifetime<'a>,
12+
marker1: marker::ContravariantLifetime<'repo>,
1313
marker2: marker::NoSend,
1414
marker3: marker::NoSync,
1515
}
1616

17-
impl<'a> Blob<'a> {
17+
impl<'repo> Blob<'repo> {
1818
/// Create a new object from its raw component.
1919
///
2020
/// This method is unsafe as there is no guarantee that `raw` is a valid
2121
/// pointer.
22-
pub unsafe fn from_raw(_repo: &Repository,
23-
raw: *mut raw::git_blob) -> Blob {
22+
pub unsafe fn from_raw(raw: *mut raw::git_blob) -> Blob<'repo> {
2423
Blob {
2524
raw: raw,
2625
marker1: marker::ContravariantLifetime,
@@ -54,7 +53,7 @@ impl<'a> Blob<'a> {
5453
}
5554

5655
#[unsafe_destructor]
57-
impl<'a> Drop for Blob<'a> {
56+
impl<'repo> Drop for Blob<'repo> {
5857
fn drop(&mut self) {
5958
unsafe { raw::git_blob_free(self.raw) }
6059
}

src/branch.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use std::kinds::marker;
12
use std::str;
23
use libc;
34

4-
use {raw, Repository, Error, Reference, Signature, BranchType};
5+
use {raw, Error, Reference, Signature, BranchType};
56

67
/// A structure to represent a git [branch][1]
78
///
@@ -15,8 +16,10 @@ pub struct Branch<'repo> {
1516

1617
/// An iterator over the branches inside of a repository.
1718
pub struct Branches<'repo> {
18-
repo: &'repo Repository,
1919
raw: *mut raw::git_branch_iterator,
20+
marker1: marker::ContravariantLifetime<'repo>,
21+
marker2: marker::NoSend,
22+
marker3: marker::NoSync,
2023
}
2124

2225
impl<'repo> Branch<'repo> {
@@ -52,7 +55,7 @@ impl<'repo> Branch<'repo> {
5255
&*signature.map(|s| s.raw())
5356
.unwrap_or(0 as *mut _),
5457
log_message.to_c_str()));
55-
Ok(Branch::wrap(Reference::from_raw_ptr(ret)))
58+
Ok(Branch::wrap(Reference::from_raw(ret)))
5659
}
5760
}
5861

@@ -78,7 +81,7 @@ impl<'repo> Branch<'repo> {
7881
let mut ret = 0 as *mut raw::git_reference;
7982
unsafe {
8083
try_call!(raw::git_branch_upstream(&mut ret, &*self.get().raw()));
81-
Ok(Branch::wrap(Reference::from_raw_ptr(ret)))
84+
Ok(Branch::wrap(Reference::from_raw(ret)))
8285
}
8386
}
8487

@@ -97,19 +100,24 @@ impl<'repo> Branch<'repo> {
97100
}
98101
}
99102

100-
impl<'a> Branches<'a> {
103+
impl<'repo> Branches<'repo> {
101104
/// Creates a new iterator from the raw pointer given.
102105
///
103106
/// This function is unsafe as it is not guaranteed that `raw` is a valid
104107
/// pointer.
105-
pub unsafe fn from_raw(repo: &Repository,
106-
raw: *mut raw::git_branch_iterator) -> Branches {
107-
Branches { repo: repo, raw: raw }
108+
pub unsafe fn from_raw(raw: *mut raw::git_branch_iterator)
109+
-> Branches<'repo> {
110+
Branches {
111+
raw: raw,
112+
marker1: marker::ContravariantLifetime,
113+
marker2: marker::NoSend,
114+
marker3: marker::NoSync,
115+
}
108116
}
109117
}
110118

111-
impl<'a> Iterator<(Branch<'a>, BranchType)> for Branches<'a> {
112-
fn next(&mut self) -> Option<(Branch<'a>, BranchType)> {
119+
impl<'repo> Iterator<(Branch<'repo>, BranchType)> for Branches<'repo> {
120+
fn next(&mut self) -> Option<(Branch<'repo>, BranchType)> {
113121
let mut ret = 0 as *mut raw::git_reference;
114122
let mut typ = raw::GIT_BRANCH_LOCAL;
115123
unsafe {
@@ -123,13 +131,13 @@ impl<'a> Iterator<(Branch<'a>, BranchType)> for Branches<'a> {
123131
raw::GIT_BRANCH_REMOTE => BranchType::Remote,
124132
raw::GIT_BRANCH_ALL => panic!("unexected branch type"),
125133
};
126-
Some((Branch::wrap(Reference::from_raw(self.repo, ret)), typ))
134+
Some((Branch::wrap(Reference::from_raw(ret)), typ))
127135
}
128136
}
129137
}
130138

131139
#[unsafe_destructor]
132-
impl<'a> Drop for Branches<'a> {
140+
impl<'repo> Drop for Branches<'repo> {
133141
fn drop(&mut self) {
134142
unsafe { raw::git_branch_iterator_free(self.raw) }
135143
}

src/commit.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,36 @@ use std::iter::Range;
33
use std::str;
44
use libc;
55

6-
use {raw, Oid, Repository, Error, Signature, Tree, Time};
6+
use {raw, Oid, Error, Signature, Tree, Time};
77

88
/// A structure to represent a git [commit][1]
99
///
1010
/// [1]: http://git-scm.com/book/en/Git-Internals-Git-Objects
11-
pub struct Commit<'a> {
11+
pub struct Commit<'repo> {
1212
raw: *mut raw::git_commit,
13-
marker1: marker::ContravariantLifetime<'a>,
13+
marker1: marker::ContravariantLifetime<'repo>,
1414
marker2: marker::NoSend,
1515
marker3: marker::NoSync,
1616
}
1717

1818
/// An iterator over the parent commits of a commit.
19-
pub struct Parents<'a, 'b:'a> {
19+
pub struct Parents<'commit, 'repo: 'commit> {
2020
range: Range<uint>,
21-
commit: &'a Commit<'b>,
21+
commit: &'commit Commit<'repo>,
2222
}
2323

2424
/// An iterator over the parent commits' ids of a commit.
25-
pub struct ParentIds<'a> {
25+
pub struct ParentIds<'commit> {
2626
range: Range<uint>,
27-
commit: &'a Commit<'a>,
27+
commit: &'commit Commit<'commit>,
2828
}
2929

30-
impl<'a> Commit<'a> {
30+
impl<'repo> Commit<'repo> {
3131
/// Create a new object from its raw component.
3232
///
3333
/// This method is unsafe as there is no guarantee that `raw` is a valid
3434
/// pointer.
35-
pub unsafe fn from_raw(_repo: &Repository,
36-
raw: *mut raw::git_commit) -> Commit {
35+
pub unsafe fn from_raw(raw: *mut raw::git_commit) -> Commit<'repo> {
3736
Commit {
3837
raw: raw,
3938
marker1: marker::ContravariantLifetime,
@@ -159,7 +158,7 @@ impl<'a> Commit<'a> {
159158
}
160159

161160
/// Creates a new iterator over the parents of this commit.
162-
pub fn parents<'b>(&'b self) -> Parents<'b, 'a> {
161+
pub fn parents<'a>(&'a self) -> Parents<'a, 'repo> {
163162
let max = unsafe { raw::git_commit_parentcount(&*self.raw) as uint };
164163
Parents {
165164
range: range(0, max),
@@ -205,7 +204,7 @@ impl<'a> Commit<'a> {
205204
committer: Option<&Signature>,
206205
message_encoding: Option<&str>,
207206
message: Option<&str>,
208-
tree: Option<&Tree<'a>>) -> Result<Oid, Error> {
207+
tree: Option<&Tree<'repo>>) -> Result<Oid, Error> {
209208
let mut raw = raw::git_oid { id: [0, ..raw::GIT_OID_RAWSZ] };
210209
unsafe {
211210
try_call!(raw::git_commit_amend(&mut raw,
@@ -223,17 +222,12 @@ impl<'a> Commit<'a> {
223222
/// Get the specified parent of the commit.
224223
///
225224
/// Use the `parents` iterator to return an iterator over all parents.
226-
pub fn parent(&self, i: uint) -> Result<Commit<'a>, Error> {
225+
pub fn parent(&self, i: uint) -> Result<Commit<'repo>, Error> {
227226
unsafe {
228227
let mut raw = 0 as *mut raw::git_commit;
229228
try_call!(raw::git_commit_parent(&mut raw, &*self.raw,
230229
i as libc::c_uint));
231-
Ok(Commit {
232-
raw: raw,
233-
marker1: marker::ContravariantLifetime,
234-
marker2: marker::NoSend,
235-
marker3: marker::NoSync,
236-
})
230+
Ok(Commit::from_raw(raw))
237231
}
238232
}
239233

@@ -255,38 +249,38 @@ impl<'a> Commit<'a> {
255249
}
256250
}
257251

258-
impl<'a, 'b> Iterator<Commit<'a>> for Parents<'b, 'a> {
259-
fn next(&mut self) -> Option<Commit<'a>> {
252+
impl<'repo, 'commit> Iterator<Commit<'repo>> for Parents<'commit, 'repo> {
253+
fn next(&mut self) -> Option<Commit<'repo>> {
260254
self.range.next().map(|i| self.commit.parent(i).unwrap())
261255
}
262256
fn size_hint(&self) -> (uint, Option<uint>) { self.range.size_hint() }
263257
}
264258

265-
impl<'a, 'b> DoubleEndedIterator<Commit<'a>> for Parents<'b, 'a> {
266-
fn next_back(&mut self) -> Option<Commit<'a>> {
259+
impl<'repo, 'commit> DoubleEndedIterator<Commit<'repo>> for Parents<'commit, 'repo> {
260+
fn next_back(&mut self) -> Option<Commit<'repo>> {
267261
self.range.next_back().map(|i| self.commit.parent(i).unwrap())
268262
}
269263
}
270264

271-
impl<'a, 'b> ExactSizeIterator<Commit<'a>> for Parents<'b, 'a> {}
265+
impl<'repo, 'commit> ExactSizeIterator<Commit<'repo>> for Parents<'commit, 'repo> {}
272266

273-
impl<'a> Iterator<Oid> for ParentIds<'a> {
267+
impl<'commit> Iterator<Oid> for ParentIds<'commit> {
274268
fn next(&mut self) -> Option<Oid> {
275269
self.range.next().map(|i| self.commit.parent_id(i).unwrap())
276270
}
277271
fn size_hint(&self) -> (uint, Option<uint>) { self.range.size_hint() }
278272
}
279273

280-
impl<'a> DoubleEndedIterator<Oid> for ParentIds<'a> {
274+
impl<'commit> DoubleEndedIterator<Oid> for ParentIds<'commit> {
281275
fn next_back(&mut self) -> Option<Oid> {
282276
self.range.next_back().map(|i| self.commit.parent_id(i).unwrap())
283277
}
284278
}
285279

286-
impl<'a> ExactSizeIterator<Oid> for ParentIds<'a> {}
280+
impl<'commit> ExactSizeIterator<Oid> for ParentIds<'commit> {}
287281

288282
#[unsafe_destructor]
289-
impl<'a> Drop for Commit<'a> {
283+
impl<'repo> Drop for Commit<'repo> {
290284
fn drop(&mut self) {
291285
unsafe { raw::git_commit_free(self.raw) }
292286
}

src/config.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ pub struct Config {
1313
/// A struct representing a certain entry owned by a `Config` instance.
1414
///
1515
/// An entry has a name, a value, and a level it applies to.
16-
pub struct ConfigEntry<'a> {
16+
pub struct ConfigEntry<'cfg> {
1717
raw: *const raw::git_config_entry,
18-
marker1: marker::ContravariantLifetime<'a>,
18+
marker1: marker::ContravariantLifetime<'cfg>,
1919
marker2: marker::NoSend,
2020
marker3: marker::NoSync,
2121
}
2222

2323
/// An iterator over the `ConfigEntry` values of a `Config` structure.
24-
pub struct ConfigEntries<'a> {
24+
pub struct ConfigEntries<'cfg> {
2525
raw: *mut raw::git_config_iterator,
26-
_config: &'a Config,
27-
marker: marker::NoSync,
26+
marker1: marker::ContravariantLifetime<'cfg>,
27+
marker2: marker::NoSend,
28+
marker3: marker::NoSync,
2829
}
2930

3031
impl Config {
@@ -187,7 +188,7 @@ impl Config {
187188
///
188189
/// This is the same as `get_bytes` except that it may return `Err` if
189190
/// the bytes are not valid utf-8.
190-
pub fn get_str<'a>(&self, name: &str) -> Result<&str, Error> {
191+
pub fn get_str(&self, name: &str) -> Result<&str, Error> {
191192
str::from_utf8(try!(self.get_bytes(name))).map_err(|_| {
192193
Error::from_str("configuration value is not valid utf8")
193194
})
@@ -209,7 +210,7 @@ impl Config {
209210
unsafe {
210211
try_call!(raw::git_config_get_entry(&mut ret, &*self.raw,
211212
name.to_c_str()));
212-
Ok(ConfigEntry::from_raw(self, ret))
213+
Ok(ConfigEntry::from_raw(ret))
213214
}
214215
}
215216

@@ -242,7 +243,7 @@ impl Config {
242243
try_call!(raw::git_config_iterator_new(&mut ret, &*self.raw));
243244
}
244245
}
245-
Ok(ConfigEntries::from_raw(self, ret))
246+
Ok(ConfigEntries::from_raw(ret))
246247
}
247248
}
248249

@@ -332,12 +333,12 @@ impl Drop for Config {
332333
}
333334
}
334335

335-
impl<'a> ConfigEntry<'a> {
336+
impl<'cfg> ConfigEntry<'cfg> {
336337
/// Creates a new config entry from the raw components.
337338
///
338339
/// This method is unsafe as the validity of `raw` is not guaranteed.
339-
pub unsafe fn from_raw(_config: &Config, raw: *const raw::git_config_entry)
340-
-> ConfigEntry {
340+
pub unsafe fn from_raw(raw: *const raw::git_config_entry)
341+
-> ConfigEntry<'cfg> {
341342
ConfigEntry {
342343
raw: raw,
343344
marker1: marker::ContravariantLifetime,
@@ -372,13 +373,18 @@ impl<'a> ConfigEntry<'a> {
372373
}
373374
}
374375

375-
impl<'a> ConfigEntries<'a> {
376+
impl<'cfg> ConfigEntries<'cfg> {
376377
/// Creates a new iterator from its raw component.
377378
///
378379
/// This function is unsafe as the validity of `raw` is not guaranteed.
379-
pub unsafe fn from_raw(config: &Config, raw: *mut raw::git_config_iterator)
380-
-> ConfigEntries {
381-
ConfigEntries { raw: raw, _config: config, marker: marker::NoSync }
380+
pub unsafe fn from_raw(raw: *mut raw::git_config_iterator)
381+
-> ConfigEntries<'cfg> {
382+
ConfigEntries {
383+
raw: raw,
384+
marker1: marker::ContravariantLifetime,
385+
marker2: marker::NoSend,
386+
marker3: marker::NoSync,
387+
}
382388
}
383389
}
384390

@@ -387,17 +393,12 @@ impl<'a> ConfigEntries<'a> {
387393
//
388394
// It's also not implemented for `&'b mut T` so we can have multiple entries
389395
// (ok).
390-
impl<'a, 'b> Iterator<ConfigEntry<'b>> for &'b ConfigEntries<'a> {
396+
impl<'cfg, 'b> Iterator<ConfigEntry<'b>> for &'b ConfigEntries<'cfg> {
391397
fn next(&mut self) -> Option<ConfigEntry<'b>> {
392398
let mut raw = 0 as *mut raw::git_config_entry;
393399
unsafe {
394400
if raw::git_config_next(&mut raw, self.raw) == 0 {
395-
Some(ConfigEntry {
396-
raw: raw as *const raw::git_config_entry,
397-
marker1: marker::ContravariantLifetime,
398-
marker2: marker::NoSend,
399-
marker3: marker::NoSync,
400-
})
401+
Some(ConfigEntry::from_raw(raw))
401402
} else {
402403
None
403404
}
@@ -406,7 +407,7 @@ impl<'a, 'b> Iterator<ConfigEntry<'b>> for &'b ConfigEntries<'a> {
406407
}
407408

408409
#[unsafe_destructor]
409-
impl<'a> Drop for ConfigEntries<'a> {
410+
impl<'cfg> Drop for ConfigEntries<'cfg> {
410411
fn drop(&mut self) {
411412
unsafe { raw::git_config_iterator_free(self.raw) }
412413
}

0 commit comments

Comments
 (0)