forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.rs
More file actions
78 lines (69 loc) · 1.65 KB
/
Copy pathutil.rs
File metadata and controls
78 lines (69 loc) · 1.65 KB
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
use bstr::BStr;
use std::alloc::Layout;
use std::marker::PhantomData;
use std::slice;
/// A string reference which may be passed over FFI boundaries.
///
/// Does not necessarily point to valid UTF8.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct FStr<'a> {
ptr: *const u8,
len: usize,
_lifetime: PhantomData<&'a [u8]>,
}
impl<'a> From<&'a BStr> for FStr<'a> {
fn from(s: &'a BStr) -> Self {
let slice: &[u8] = s.as_ref();
Self::from(slice)
}
}
impl<'a> From<&'a [u8]> for FStr<'a> {
fn from(slice: &'a [u8]) -> Self {
Self {
ptr: slice.as_ptr(),
len: slice.len(),
_lifetime: PhantomData,
}
}
}
impl<'a> From<&'a str> for FStr<'a> {
fn from(string: &'a str) -> Self {
Self {
ptr: string.as_ptr(),
len: string.len(),
_lifetime: PhantomData,
}
}
}
impl<'a> From<FStr<'a>> for &'a BStr {
fn from(string: FStr<'a>) -> Self {
let slice = <&[u8]>::from(string);
slice.into()
}
}
impl<'a> From<FStr<'a>> for &'a [u8] {
fn from(string: FStr<'a>) -> Self {
unsafe { slice::from_raw_parts(string.ptr, string.len) }
}
}
/// An FFI-safe version of `std::allloc::Layout`.
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct FLayout {
size: usize,
align: usize,
}
impl From<Layout> for FLayout {
fn from(layout: Layout) -> Self {
Self {
size: layout.size(),
align: layout.align(),
}
}
}
impl From<FLayout> for Layout {
fn from(layout: FLayout) -> Self {
Layout::from_size_align(layout.size, layout.align).unwrap()
}
}