owning.rs
91 lines
| 2.3 KiB
| application/rls-services+xml
|
RustLexer
|
r49864 | use crate::{DirstateError, DirstateParents}; | ||
|
r48766 | use super::dirstate_map::DirstateMap; | ||
|
r51575 | use self_cell::self_cell; | ||
|
r48766 | use std::ops::Deref; | ||
|
r51575 | self_cell!( | ||
/// Keep a `DirstateMap<'owner>` next to the `owner` buffer that it | ||||
/// borrows. | ||||
pub struct OwningDirstateMap { | ||||
owner: Box<dyn Deref<Target = [u8]> + Send>, | ||||
#[covariant] | ||||
dependent: DirstateMap, | ||||
} | ||||
); | ||||
|
r48766 | |||
impl OwningDirstateMap { | ||||
pub fn new_empty<OnDisk>(on_disk: OnDisk) -> Self | ||||
where | ||||
|
r49864 | OnDisk: Deref<Target = [u8]> + Send + 'static, | ||
|
r48766 | { | ||
let on_disk = Box::new(on_disk); | ||||
|
r51575 | OwningDirstateMap::new(on_disk, |bytes| DirstateMap::empty(bytes)) | ||
|
r48766 | } | ||
|
r49864 | pub fn new_v1<OnDisk>( | ||
on_disk: OnDisk, | ||||
|
r51140 | identity: Option<u64>, | ||
|
r49864 | ) -> Result<(Self, DirstateParents), DirstateError> | ||
where | ||||
OnDisk: Deref<Target = [u8]> + Send + 'static, | ||||
{ | ||||
let on_disk = Box::new(on_disk); | ||||
let mut parents = DirstateParents::NULL; | ||||
|
r48766 | |||
|
r49864 | Ok(( | ||
|
r51575 | OwningDirstateMap::try_new(on_disk, |bytes| { | ||
DirstateMap::new_v1(bytes, identity).map(|(dmap, p)| { | ||||
parents = p.unwrap_or(DirstateParents::NULL); | ||||
dmap | ||||
}) | ||||
})?, | ||||
|
r49864 | parents, | ||
)) | ||||
|
r48766 | } | ||
|
r49864 | pub fn new_v2<OnDisk>( | ||
on_disk: OnDisk, | ||||
data_size: usize, | ||||
metadata: &[u8], | ||||
|
r51138 | uuid: Vec<u8>, | ||
|
r51140 | identity: Option<u64>, | ||
|
r49864 | ) -> Result<Self, DirstateError> | ||
where | ||||
OnDisk: Deref<Target = [u8]> + Send + 'static, | ||||
{ | ||||
let on_disk = Box::new(on_disk); | ||||
|
r51575 | OwningDirstateMap::try_new(on_disk, |bytes| { | ||
DirstateMap::new_v2(bytes, data_size, metadata, uuid, identity) | ||||
}) | ||||
|
r48766 | } | ||
|
r49864 | pub fn with_dmap_mut<R>( | ||
&mut self, | ||||
f: impl FnOnce(&mut DirstateMap) -> R, | ||||
) -> R { | ||||
|
r51575 | self.with_dependent_mut(|_owner, dmap| f(dmap)) | ||
|
r49864 | } | ||
pub fn get_map(&self) -> &DirstateMap { | ||||
|
r51575 | self.borrow_dependent() | ||
|
r49864 | } | ||
pub fn on_disk(&self) -> &[u8] { | ||||
|
r51575 | self.borrow_owner() | ||
|
r48766 | } | ||
|
r51138 | |||
pub fn old_uuid(&self) -> Option<&[u8]> { | ||||
self.get_map().old_uuid.as_deref() | ||||
} | ||||
|
r51140 | pub fn old_identity(&self) -> Option<u64> { | ||
self.get_map().identity | ||||
} | ||||
|
r51138 | pub fn old_data_size(&self) -> usize { | ||
self.get_map().old_data_size | ||||
} | ||||
|
r48766 | } | ||