9

Partially stabilize const_slice_from_raw_parts by xfix · Pull Request #97522 · r...

 2 years ago
source link: https://github.com/rust-lang/rust/pull/97522
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Contributor

@xfix xfix commented on May 29

edited

This doesn't stabilize methods working on mutable pointers.

This pull request continues from #94946.

Pinging @rust-lang/wg-const-eval this because I use rustc_allow_const_fn_unstable. I believe this is justifiable as it's already possible to use slice::from_raw_parts in stable by abusing transmute. The stable alternative to this would be to provide a stable const implementation of std::ptr::from_raw_parts (as it can already be implemented in stable).

use std::mem;

#[repr(C)]
struct Slice<T> {
    data: *const T,
    len: usize,
}

fn main() {
    let data: *const i32 = [1, 2, 3, 4].as_ptr();
    let len = 4;
    println!("{:?}", unsafe {
        mem::transmute::<Slice<i32>, &[i32]>(Slice { data, len })
    });
}

@rustbot modify labels: +T-libs-api

All reactions

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK