git2/
oid_array.rs

1//! Bindings to libgit2's raw `git_oidarray` type
2
3use std::ops::Deref;
4
5use crate::oid::Oid;
6use crate::raw;
7use crate::util::Binding;
8use std::mem;
9use 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.
16pub struct OidArray {
17    raw: raw::git_oidarray,
18}
19
20impl Deref for OidArray {
21    type Target = [Oid];
22
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
32impl Binding for OidArray {
33    type Raw = raw::git_oidarray;
34    unsafe fn from_raw(raw: raw::git_oidarray) -> OidArray {
35        OidArray { raw }
36    }
37    fn raw(&self) -> raw::git_oidarray {
38        self.raw
39    }
40}
41
42impl<'repo> std::fmt::Debug for OidArray {
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
48impl Drop for OidArray {
49    fn drop(&mut self) {
50        unsafe { raw::git_oidarray_free(&mut self.raw) }
51    }
52}