-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement procedural macro diagnostics
commit-id:f06e9653
- Loading branch information
Showing
9 changed files
with
507 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/// This struct encapsulates a stable ABI representation of information required to construct a slice. | ||
/// | ||
/// Please be aware that the memory management of values passing the FFI-barrier is tricky. | ||
/// The memory must be freed on the same side of the barrier, where the allocation was made. | ||
#[repr(C)] | ||
#[derive(Debug)] | ||
#[doc(hidden)] | ||
pub struct StableSlice<T> { | ||
ptr: *mut T, | ||
len: usize, | ||
} | ||
|
||
impl<T> StableSlice<T> { | ||
/// Create a new `StableSlice` from a Vector. | ||
/// Note that the vector will not be deallocated automatically. | ||
/// Please make sure to use `into_owned` afterward, to free the memory. | ||
pub fn new(mut x: Vec<T>) -> Self { | ||
x.shrink_to_fit(); | ||
assert_eq!(x.len(), x.capacity()); | ||
let ptr = x.as_mut_ptr(); | ||
let len = x.len(); | ||
std::mem::forget(x); | ||
Self { ptr, len } | ||
} | ||
|
||
/// Convert to owned vector. | ||
pub fn into_owned(self) -> Vec<T> { | ||
unsafe { Vec::from_raw_parts(self.ptr, self.len, self.len) } | ||
} | ||
|
||
/// Returns raw pointer and length. | ||
/// Can be used to construct a slice. | ||
/// No ownership is transferred. | ||
pub fn raw_parts(&self) -> (*mut T, usize) { | ||
(self.ptr, self.len) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.