#How to write unsafe function?

12 messages · Page 1 of 1 (latest)

north osprey
#

Hello guys! So this is my current code:

    fn collect_page_tables(&self, page: &Page) -> Option<PageTables> {
        // ...
        while ... {
            // ...
            let table_entry = unsafe { &(*pt_ptr)[entry_index] };
            // ...
        }
        // ...
    }

Am I using unsafe correctly here or should I rather mark the whole function as unsafe?

#

Because I'm unsure when I should mark a function as unsafe and when just a piece of code

opal anvil
#

If so, then the function is unsafe

#

Else, it uses unsafe, but it acts as a safe abstraction

north osprey
#

I think that marking the whole function as unsafe is the appropriate way to do this. Thank you!

#

@opal anvil oh eh, one little question: If I mark a function as unsafe, is it fine/recommend to mark the specifique code-place as unsafe which makes the whole function unsafe?

#

something like:

unsafe sus() {
    // ...
    unsafe {
        // here happens the unsafe stuff
    }
    // ...
}
opal anvil
north osprey
#

cool

#

thank you!