Using "as *const()" on structure method that creates initial structure instance

I have a structure and method like so

#[derive(Clone, Copy)]
pub struct TrapFrame{
    pub regs : [usize;32]

impl TrapFrame {
    pub const fn new() -> self {
            regs: [0;32]

I want a new instance of the structure, then set the memory address to a register

let user_frame = TrapFrame::new();
let user_frame_ptr = &user_frame as *const();
mscratch::write(user_frame_ptr as usize);

This gives the following error

casting &TrapFrame as *const() is invalid

What's my problem?

You're trying to cast with two types of coercion (I think this should be supported in the future - "transitive case").
Try splitting them into separate parts:

// step 1: cast &T to *const T
// step 2: cast *const T to *const U
let user_frame_ptr = &user_frame as *const TrapFrame as *const ();

Read more about this here.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.