1 //! Bindings to libgit2's raw `git_oidarray` type
2 
3 use std::ops::Deref;
4 
5 use crate::oid::Oid;
6 use crate::raw;
7 use crate::util::Binding;
8 use std::mem;
9 use std::slice;
10 
11 /// An oid array structure used by libgit2
12 ///
13 /// Some apis return arrays of oids which originate from libgit2. This
14 /// wrapper type behaves a little like `Vec<&Oid>` but does so without copying
15 /// the underlying Oids until necessary.
16 pub struct OidArray {
17     raw: raw::git_oidarray,
18 }
19 
20 impl Deref for OidArray {
21     type Target = [Oid];
22 
deref(&self) -> &[Oid]23     fn deref(&self) -> &[Oid] {
24         unsafe {
25             debug_assert_eq!(mem::size_of::<Oid>(), mem::size_of_val(&*self.raw.ids));
26 
27             slice::from_raw_parts(self.raw.ids as *const Oid, self.raw.count as usize)
28         }
29     }
30 }
31 
32 impl Binding for OidArray {
33     type Raw = raw::git_oidarray;
from_raw(raw: raw::git_oidarray) -> OidArray34     unsafe fn from_raw(raw: raw::git_oidarray) -> OidArray {
35         OidArray { raw }
36     }
raw(&self) -> raw::git_oidarray37     fn raw(&self) -> raw::git_oidarray {
38         self.raw
39     }
40 }
41 
42 impl<'repo> std::fmt::Debug for OidArray {
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error>43     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
44         f.debug_tuple("OidArray").field(&self.deref()).finish()
45     }
46 }
47 
48 impl Drop for OidArray {
drop(&mut self)49     fn drop(&mut self) {
50         unsafe { raw::git_oidarray_free(&mut self.raw) }
51     }
52 }
53