News for hackers. Stuff that matters.
Re:The rise of wgpu by flohofwoe on Thursday March 07, 2019 @06:23AM (#19327203)
Worth looking at the hello-triangle examples for the C- and Rust-APIs:

C99 Hello Triangle:

https://github.com/gfx-rs/wgpu/blob/master/examples/hello_tr...

Rust Hello Triangle:

https://github.com/gfx-rs/wgpu/blob/master/examples/hello_tr...

Both look very neat and tidy IMHO.

The C99 version could also use nested designated initialization right in the function call, this would make it look very similar to the Rust version, e.g.:

    WGPUBlendStateId blend_state_0 = wgpu_device_create_blend_state(
        device,
        &(WGPUBlendDescriptor) {
            .blend_enabled = false,
            .write_mask = WGPUColorWriteFlags_ALL,
            .color = {
                .src_factor = WGPUBlendFactor_One,
                .dst_factor = WGPUBlendFactor_Zero,
                .operation = WGPUBlendOperation_Add,
            },
            .alpha = {
                .src_factor = WGPUBlendFactor_One,
                .dst_factor = WGPUBlendFactor_Zero,
                .operation = WGPUBlendOperation_Add,
            }
        });
But of course that's a matter of taste :)
Re:The rise of wgpu by mrec on Thursday March 07, 2019 @06:38AM (#19327255)
I was about to say "yeah, you should look at flohofwoe's Sokol stuff for an example of nested init", then saw the username :)
Re:The rise of wgpu by skocznymroczny on Thursday March 07, 2019 @08:25AM (#19327799)
For me, the C version looks actually cleaner than the Rust one.
Re:The rise of wgpu by bluejekyll on Thursday March 07, 2019 @09:15AM (#19328113)
Is this because you’re not as familiar with Rust as you are with C?

Rust has more syntactic elements than the C, in this code that’s clear, but cleanliness can mean different things.

Rust has a module system, so no global namespace issues, but does require all of the :: separators.

Rust has cleaner ways of dealing with cross platform compile time switches, thus we don’t see the CPP macros for different platform targets.

To me, those are cleaner, but I’m familiar with Rust and C, which is why I asked.

Re:The rise of wgpu by alkonaut on Thursday March 07, 2019 @12:49PM (#19330229)
I think the Rust style that uses ”constructorless” struct initialization really makes the code too verbose in this case. It’s nice and explicit in a way but having to say e.g

    entrypoint: ”main”
as the argument for a shader program seems like it could be turned into a default.

Convention over configuration isn’t always the answer, but some times at least it can help reduce clutter.

In the future the api will perhaps have builders as an option.

Re:The rise of wgpu by flohofwoe on Thursday March 07, 2019 @01:02PM (#19330371)
I'm not sure if the builder pattern is better than designated initialization... AFAIK in Rust you can get something similar (fill up missing members with their default values) with ..Default::default(), like:

    &BlaDesc {
        bla: 1,
        blub: 2,
        ..Default::default()
    }
Re:The rise of wgpu by masklinn on Thursday March 07, 2019 @01:31PM (#19330684)
One annoyance of this is that you can't require fields to be explicitly provided anymore.

With the builder pattern, you can do so: require them in the builder constructor.

Re:The rise of wgpu by kvark on Thursday March 07, 2019 @08:56AM (#19327996)
I'm genuinely interested in why you find it cleaner, please elaborate?
Re:The rise of wgpu by gpm on Thursday March 07, 2019 @09:40AM (#19328329)
Three thoughts:

- The types help, seeing "WGPUSwapChainId" tells me exactly what sort of object is returned (a reference to some object held elsewhere), we don't get this in Rust because of type inference.

- Rust doesn't help in this example because it's just a bunch of API calls... so it's a more complex language for nothing. Obviously this doesn't generalize.

- The event handling is much simpler in the C code, this also doesn't generalize.

Re:The rise of wgpu by mrec on Thursday March 07, 2019 @12:51PM (#19330249)
> we don't get this in Rust because of type inference

Might be worth mentioning that (AFAIK) type inference is entirely optional in Rust. If you prefer to write explicit types on your decls that's fine. (I suspect there are pathological cases where the type is impractical to write, but you'd have been screwed then anyway.)

This won't help you when reading other people's code, obviously.

Re:The rise of wgpu by asddgsgsaxg on Thursday March 07, 2019 @11:26AM (#19329324)
> seeing "WGPUSwapChainId" tells me exactly what sort of object is returned (a reference to some object held elsewhere)

Check out RLS (the rust language server) to get intellisense like this working in your favorite editor.

Re:The rise of wgpu by jonathanyc on Thursday March 07, 2019 @11:55AM (#19329632)
I think that in reviewing code this is definitely something where languages with type inference lose clarity; Rust is even worse because of the Deref/Into idioms.

I think the solution is for diff viewers incl. those on the web to support something like intellisense, but obviously we are a very long way away from that.

Re:The rise of wgpu by devit on Thursday March 07, 2019 @12:05PM (#19329733)
You could have the compiler produce a fully explicit version (I'm not sure if Rust can do that out of the box at the moment)
Re:The rise of wgpu by steveklabnik on Thursday March 07, 2019 @12:08PM (#19329761)
~~It can't.~~ See below, it can!

I tend to find, personally, that this kind of thing doesn't interfere strongly with reviewing. I think this is because I have an extensive background in dynamically typed languages and ones with strong inference like Rust, so I'm more used to it, whereas people who don't come from said backgrounds prefer more explicitness. YMMV of course!

Re:The rise of wgpu by Tohquai7 on Thursday March 07, 2019 @01:05PM (#19330397)
It can.

-Z unstable-options -Zunpretty=hir,typed

    // source
    pub fn square(num: i32) -> i32 {
        num * num
    }

    // expanded
    use ::std::prelude::v1::*;
    extern crate std;
    pub fn square(num: i32)
     -> i32 ({ ((num as i32) * (num as i32) as i32) } as i32)

You can try on https://rust.godbolt.org/
Re:The rise of wgpu by steveklabnik on Thursday March 07, 2019 @01:07PM (#19330420)
Oh dang! That's very cool, thank you!
Re:The rise of wgpu by jonathanyc on Thursday March 07, 2019 @01:05PM (#19330396)
I don’t think it’s just “strong” inference that’s the problem; I find I have an easier time reading ML because the inference somehow feels more obvious. After thinking about it some more, I think it really is deref coercion that is the gotcha, especially with function chaining.
Re:The rise of wgpu by steveklabnik on Thursday March 07, 2019 @01:08PM (#19330429)
By "strong" I really mean "type inference" over "type deduction", to be clear. But yeah, I could see that.
Re:The rise of wgpu by singularity2001 on Thursday March 07, 2019 @07:54PM (#19334206)
>> very neat and tidy

I couldn't disagree more.

Re:The rise of wgpu by flohofwoe on Friday March 08, 2019 @05:10AM (#19336377)
It's definitely cleaner and less code compared to a Vulkan "hello triangle".

Compared to D3D11 or Metal? Arguable.

Compared to GL? For a simple triangle also arguable, but GL gets messier much quicker for complex cases.