-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code generation for the slice type
This type must respect the layout of the FatPtr type in libcore. Rust implements slices using Rustc types in libcore and uses a neat trick. The slice is generated into the FatPtr which contains the pointer and length of the slice. This is then placed into a union called Repr which has 3 variants a mutable and immutable pointer to the FatPtr and a final variant which is the raw FatPtr. This means we can use unsafe access to the union to gain a pointer to the FatPtr. Addresses #849
- Loading branch information
Showing
2 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
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,27 @@ | ||
// { dg-additional-options "-w" } | ||
struct FatPtr<T> { | ||
data: *const T, | ||
len: usize, | ||
} | ||
|
||
union Repr<T> { | ||
rust: *const [T], | ||
rust_mut: *mut [T], | ||
raw: FatPtr<T>, | ||
} | ||
|
||
const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] { | ||
unsafe { | ||
let a = FatPtr { data, len }; | ||
let b = Repr { raw: a }; | ||
b.rust | ||
} | ||
} | ||
|
||
fn main() -> i32 { | ||
let a = 123; | ||
let b: *const i32 = &a; | ||
let c = slice_from_raw_parts(b, 1); | ||
|
||
0 | ||
} |