yes
yes
16.67%
1
no (bad)
83.33%
5
6 vote(s)
* You voted for this item.

Rust?

1 Replies, 508 Views

Curious on people's thoughts on a new language I'm working on, posted some initial work here


Attached Files .zip   rust-master.zip
(06-15-2023, 12:29 PM)Forust Wrote: Curious on people's thoughts on a new language I'm working on, posted some initial work here

An interesting start to a language for sure. While the compiler is a bit pedantic, it is largely an OK developer experience. I found that the trick to using this language is whenever you want to do crimes with memory, just wrap it in unsafe. For ergonomics, I found that writing unsafe everywhere to be a little less comfortable.

Luckily this language seems to have a pretty good macro system, I found that I could write a macro that will do the function wrapping for me:

Code:
macro_rules! wrap_unsafe {
    ($($func:ident ( $($param:ident : $param_type:ty),* ) $(-> $return_type:ty)? $body:block)*) => {
        $(
            fn $func($($param : $param_type),*) $(-> $return_type)? {
                unsafe { $body }
            }
        )*
    };
}

This way, memory crimes are just more ergonomic and comfortable.

Code:
wrap_unsafe! {
    unsafe_example(buffer: &mut [u8; 2]) -> u32 {
        let buffer_ptr = buffer.as_mut_ptr();
   
        // Offset is unsafe
        *buffer_ptr.offset(0) = 0xAA;
        ptr_magic_value(buffer_ptr.offset(1));
        buffer[0] as u32 + buffer[1] as u32
    }
    ptr_magic_value(ptr: *mut u8) {
        // Deref of raw pointer
        *ptr = 42;
    }
}

fn main() {
    let mut buffer: [u8; 2] = [0, 0];
    println!("{}", unsafe_example(&mut buffer));
}



Users browsing this thread: 1 Guest(s)