aloso

joined 1 year ago
[–] aloso@programming.dev 2 points 1 year ago (7 children)

Discriminant is irrelevant and you’re not supposed to fuck with it

It matters because the conversion between i32 and the Result is only "free" if they have the same layout (which they do not, because of the discriminant). So a more costly conversion method is required.

And there is zero reason to use unsafe/transmute for this.

You are right, because the compiler is able to optimize your code quite well. However, if that optimization were to break at some point (as there is no guarantee that an optimization will continue to work in the future), it would become less efficient.

[–] aloso@programming.dev 3 points 1 year ago* (last edited 1 year ago) (10 children)

This is not possible, because Rust still stores a discriminant even when the enum values don't overlap.

As far as I can tell, the only situation where Rust doesn't store a discriminant is when either the Ok or Err variant is zero-sized, and the other variant has a niche. So, Result<(), ErrorEnum> can be represented as an integer, but Result can not.

You can still use enums, and implement simple conversions like this:

#[repr(i8)]
pub enum Error {
    E1 = -1,
    E2 = -2,
    E3 = -3,
    E4 = -4,
}

#[repr(i8)]
pub enum Success {
    S0 = 0,
    S1 = 1,
    S2 = 2,
    S3 = 3,
}

pub type LibResult = Result;

pub fn number_to_result(value: i32) -> Option {
    match value {
        -4 ..= -1 => Some(Err(unsafe { std::mem::transmute(value as i8) })),
        0 ..= 3 => Some(Err(unsafe { std::mem::transmute(value as i8) })),
        _ => return None,
    }
}

pub fn result_to_number(res: LibResult) -> i32 {
    match res {
        Ok(value) => value as i32,
        Err(error) => error as i32,
    }
}

P.S. Sorry that the generics aren't displayed due to Lemmy's bad santiziation.

[–] aloso@programming.dev 12 points 1 year ago* (last edited 1 year ago)

Iframes cannot access the main frame's DOM if the iframe is from a different origin than the main frame, and they never share the same JavaScript execution context, so an iframe can't access the main frame's variables etc.

It's not required that iframes run in a different process, but I think they do at least in Chrome and Firefox if they're from a different origin. Also, iframes with the sandbox attribute have a number of additional restrictions, which can be individually disabled when needed.

[–] aloso@programming.dev 13 points 1 year ago (2 children)

They still have their place; for example to embed Google Maps or a YouTube video. Generally, whenever you want to embed something from a different website you have no control over, that shouldn't inherit your style sheets, and should be sandboxed to prevent cross site scripting attacks.

[–] aloso@programming.dev 4 points 1 year ago (5 children)

True, code for critical IT infrastructure should always be reviewed. But from what I understand, this is difficult because there is one full-time developer (paid by the Rust Foundation) and a small number of volunteers, who don't have the time to review all the employee's changes.

[–] aloso@programming.dev 3 points 1 year ago (1 children)

When I searched "bitcoin", I found the bitcoin crate, but I had to scroll down a bit.

[–] aloso@programming.dev 7 points 1 year ago (1 children)

because it's really not that much better than crates.io and it has the downside of the results being biased.

I use its search, because it produces much better search results. The crates.io search is almost unusable, it rarely finds anything useful in the top search results.

[–] aloso@programming.dev 3 points 1 year ago* (last edited 1 year ago)

You confused revenue and profit. You must subtract expenses to calculate the profit. For example, if you buy something for $20 and sell it for $21, your revenue is $21, but your profit is only $1.

Facebook reported a profit of $39 billion in 2021 and $23 billion in 2022. This takes their expenses (salaries, offices, data centres, etc.) into account.

[–] aloso@programming.dev 1 points 1 year ago

Sure, but raw pointers and unsafe Rust are still covered in the official learning material, so I assume that most Rust devs know about raw pointers.

[–] aloso@programming.dev 2 points 1 year ago* (last edited 1 year ago) (1 children)

I can recommend Lua. It is easy to learn, and easy to embed in a Rust program. With LuaJIT, it should be pretty fast, too.

Of course you can also embed a JavaScript runtime, but then your executable will probably be 50 MB larger. And I'm not a fan of Python.

[–] aloso@programming.dev 2 points 1 year ago

The reddit thread has some interesting discussion, and a solution using no SIMD intrinsincs that is more than 200x faster, by using .chunks_exact(), and letting the compiler auto-vectorize it.

view more: ‹ prev next ›