1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2021 Ian Jackson and contributors
// SPDX-License-Identifier: GPL-3.0-or-later
// There is NO WARRANTY.

//! Work in progress

#[cfg(test)] mod test;

#[allow(non_camel_case_types)]
pub trait PartialBorrow {
  type All_Mut;
  type All_Ref;
  type All_Not;
  type Fields;
  const FIELDS: Self::Fields;
}

pub trait Downgrade<R> {
  fn downgrade    (input: &Self    ) -> &    R;
  fn downgrade_mut(input: &mut Self) -> &mut R;
}
pub trait SplitOff<R> {
  type Remaining;
  fn split_off    (input: &    Self) -> (&    R, &    Self::Remaining);
  fn split_off_mut(input: &mut Self) -> (&mut R, &mut Self::Remaining);
}
pub trait SplitInto<R,S> {
  fn split_into    (input: &    Self) -> (&    R, &    S);
  fn split_into_mut(input: &mut Self) -> (&mut R, &mut S);
}

pub use partial_borrow_macros::{PartialBorrow, partial};

pub mod prelude {
  pub use crate as partial_borrow;
  pub use super::{PartialBorrow, partial};
}

pub mod perms {
  #[derive(Debug,Clone,Copy)] pub struct Not;
  #[derive(Debug,Clone,Copy)] pub struct Ref;
  #[derive(Debug)]            pub struct Mut;

  pub unsafe trait IsRef { }
  unsafe impl IsRef for Ref { }
  unsafe impl IsRef for Mut { }

  pub unsafe trait IsMut: IsRef { }
  unsafe impl IsMut for Mut { }

  pub unsafe trait IsRefOrNot { }
  unsafe impl IsRefOrNot for Not { }
  unsafe impl IsRefOrNot for Ref { }
  unsafe impl IsRefOrNot for Mut { }

  pub unsafe trait IsDowngradeFrom<P> { type Remaining: IsRefOrNot; }
  unsafe impl IsDowngradeFrom<Mut> for Mut { type Remaining = Not; }
  unsafe impl IsDowngradeFrom<Mut> for Ref { type Remaining = Ref; }
  unsafe impl IsDowngradeFrom<Mut> for Not { type Remaining = Mut; }
  //
  unsafe impl IsDowngradeFrom<Ref> for Ref { type Remaining = Ref; }
  unsafe impl IsDowngradeFrom<Ref> for Not { type Remaining = Ref; }
  //
  unsafe impl IsDowngradeFrom<Not> for Not { type Remaining = Not; }

  pub unsafe trait CanSplitInto<R,S> { }
  unsafe impl CanSplitInto<Mut,Not> for Mut { }
  unsafe impl CanSplitInto<Not,Not> for Mut { }
  //
  unsafe impl CanSplitInto<Ref,Ref> for Mut { }
  unsafe impl CanSplitInto<Ref,Not> for Mut { }
  unsafe impl CanSplitInto<Not,Mut> for Mut { }
  unsafe impl CanSplitInto<Not,Ref> for Mut { }
  //
  unsafe impl CanSplitInto<Ref,Ref> for Ref { }
  unsafe impl CanSplitInto<Ref,Not> for Ref { }
  unsafe impl CanSplitInto<Not,Ref> for Ref { }
  unsafe impl CanSplitInto<Not,Not> for Ref { }

  unsafe impl CanSplitInto<Not,Not> for Not { }

  pub trait Adjust<P, const FI: usize>  {
    type Adjusted;
  }
}

pub use memoffset;