git2/
mempack.rs

1use std::marker;
2
3use crate::util::Binding;
4use crate::{raw, Buf, Error, Odb, Repository};
5
6/// A structure to represent a mempack backend for the object database. The
7/// Mempack is bound to the Odb that it was created from, and cannot outlive
8/// that Odb.
9pub struct Mempack<'odb> {
10    raw: *mut raw::git_odb_backend,
11    _marker: marker::PhantomData<&'odb Odb<'odb>>,
12}
13
14impl<'odb> Binding for Mempack<'odb> {
15    type Raw = *mut raw::git_odb_backend;
16
17    unsafe fn from_raw(raw: *mut raw::git_odb_backend) -> Mempack<'odb> {
18        Mempack {
19            raw,
20            _marker: marker::PhantomData,
21        }
22    }
23
24    fn raw(&self) -> *mut raw::git_odb_backend {
25        self.raw
26    }
27}
28
29// We don't need to implement `Drop` for Mempack because it is owned by the
30// odb to which it is attached, and that will take care of freeing the mempack
31// and associated memory.
32
33impl<'odb> Mempack<'odb> {
34    /// Dumps the contents of the mempack into the provided buffer.
35    pub fn dump(&self, repo: &Repository, buf: &mut Buf) -> Result<(), Error> {
36        unsafe {
37            try_call!(raw::git_mempack_dump(buf.raw(), repo.raw(), self.raw));
38        }
39        Ok(())
40    }
41
42    /// Clears all data in the mempack.
43    pub fn reset(&self) -> Result<(), Error> {
44        unsafe {
45            try_call!(raw::git_mempack_reset(self.raw));
46        }
47        Ok(())
48    }
49}