binbuf/
entry.rs

1use std::cmp::Ordering;
2
3pub use crate::bytes_ptr as ptr;
4pub use ptr::Instance as Ptr;
5
6pub type Buf<T: Instance, P> = T::Buf<P>;
7pub type BufConst<T: Instance> = T::Buf<ptr::Const>;
8pub type BufMut<T: Instance> = T::Buf<ptr::Mut>;
9
10pub fn buf_to_const<T: Instance, P: Ptr>(buf: Buf<T, P>) -> BufConst<T> {
11    unsafe { T::buf(T::buf_ptr(buf).to_const()) }
12}
13
14pub unsafe fn buf_from_slice<T: Instance>(slice: &[u8]) -> BufConst<T> {
15    T::buf(ptr::Const::from_slice(slice))
16}
17
18pub unsafe fn buf_mut_from_slice<T: Instance>(slice: &mut [u8]) -> BufMut<T> {
19    T::buf(ptr::Mut::from_slice(slice))
20}
21
22pub trait Instance {
23    type Buf<P: Ptr>: Clone + Copy;
24    // Caller must ensure ptr is of correct length.
25    unsafe fn buf<P: Ptr>(ptr: P) -> Self::Buf<P>;
26    fn buf_ptr<P: Ptr>(buf: Self::Buf<P>) -> P;
27}
28
29// pub trait BufPartialOrd: Instance {
30//     fn buf_partial_cmp<P: Ptr>(a: Buf<Self, P>, b: Buf<Self, P>) -> Option<Ordering>;
31// }
32
33// pub trait BufOrd: BufPartialOrd {
34//     fn buf_cmp<P: Ptr>(a: Buf<Self, P>, b: Buf<Self, P>) -> Option<Ordering>;
35// }