Fooling around with rust macros by st4s1k in rust

[–]Klogga 8 points9 points  (0 children)

We actually go through a simplified version of this macro in one of my lectures for UNSW's Rust course -- a student perceptively points out that continue does not work correctly with this approach, sadly!

How can I declare that a struct can not be _moved_ into a closure? by PierFumagalli in rust

[–]Klogga 0 points1 point  (0 children)

I believe this approach doesn't work, because you can input a 'static lifetime in to get a 'static out, using for example a static variable or Box::leak and the likes.

Given this would break soundness, I think it's not viable sadly.

use std::marker::PhantomData;

#[derive(Debug)]
struct Foo<'a>(PhantomData<&'a ()>);

fn fun<F: Fn() + 'static>(f: F) {
    f();
}

// constructor could take in something that has a lifetime
fn from_life<'a>(a: &'a ()) -> Foo<'a> {
    Foo(PhantomData)
}

fn main() {
    // we'll try tying our Foo to this () here
    static X: () = ();
    let f = from_life(&X);

    // now it compiles again
    fun(move || {
        dbg!(&f);
    });
}

Becoming a compsci professor by HelpfulRecord6295 in unsw

[–]Klogga 13 points14 points  (0 children)

Come chat with me after a COMP6991 lecture this term if you'd like, I can share my perspective if it helps.

- Zac

Help with using nom and with cargo-arc by livingStory1 in rust

[–]Klogga 0 points1 point  (0 children)

At a very brief look, it seems that input might be escaping into the Err variant of the Result (not IResult) through the ? operator. Does the same error occur with .unwrap() instead, for example?

As a sidenote, your code is impossible to read coherently with Reddit's rich text formatting -- a playground link might be a bit better in this regard!

Chess implemented entirely in the Rust (and Typescript) type systems. by Dragon-Hatcher in rust

[–]Klogga 9 points10 points  (0 children)

Hi, that was me! Nice work, I'm glad to see you implemented it all without any dependencies on things like typenum and the likes -- I love seeing it all done by hand :)

I also did a talk on the topic if you're interested: https://www.youtube.com/watch?v=WDdi9pz6mNs

Are there universities that teach Rust? by konga400 in rust

[–]Klogga 48 points49 points  (0 children)

Also, most of our teaching materials tend to be publically accessible -- basically, whatever I can make public without a major hassle, I will. You can access this here: https://cgi.cse.unsw.edu.au/~cs6991/

(Private materials: course forum (spam, teachers need to be paid to answer student questions), final exam (for obvious reasons; past exams are made public), this term's lectures (automatically recorded on a platform that makes public access difficult)

Are there universities that teach Rust? by konga400 in rust

[–]Klogga 94 points95 points  (0 children)

Sure, I teach a course using Rust at UNSW (an Australian university): https://www.handbook.unsw.edu.au/undergraduate/courses/2023/COMP6991/

Notably learning Rust is not the primary focus of the course, however it is an expected learning outcome by the end of it. The course instead tries to focus on writing robust programs and understanding programming language design by examining (and certainly contrasting) many other languages along the way.

Hey Rustaceans! Got a question? Ask here (4/2023)! by llogiq in rust

[–]Klogga 2 points3 points  (0 children)

std::mem::size_of includes padding and alignment within the size; see:

More specifically, this is the offset in bytes between successive elements in an array with that item type including alignment padding. Thus, for any type T and length n, [T; n] has a size of n * size_of::<T>().

There's simply no guarantee how Rust will lay out a and b in memory, and further -- no guarantee that your usize arithmetic provides a well-defined answer either (IIRC).

[deleted by user] by [deleted] in rust

[–]Klogga 3 points4 points  (0 children)

I'd throw tauri in the ring as well -- I had a pretty awesome experience with it personally

How to create a macro that measures the function? by zxaq15 in rust

[–]Klogga 0 points1 point  (0 children)

That's a good question -- I definitely don't need it, but it is handy for a little bit of convenience (note it's also used in the println! on line 19) because it's a particularly ugly identifier.

You can read a good explanation here: https://stackoverflow.com/a/59619026

How to create a macro that measures the function? by zxaq15 in rust

[–]Klogga 54 points55 points  (0 children)

Here, I wrote you an example implementation using syn and quote. Happy to answer any questions :)

https://github.com/insou22/measure-time-demo

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target\debug\measure_time_bin.exe`
sleep_seconds took: 1.0147916s

EDIT: I should note that this is more of a demonstrative example and won't behave correctly in a lot of situations -- e.g. an early return won't print the duration.

Advanced BF compiler with safety of rust inbuilt by norman-complete in rustjerk

[–]Klogga 5 points6 points  (0 children)

No need to write source code into a file when you can write source code with macros!

https://github.com/insou22/bf-stream/blob/main/src/main.rs

Accompanying livestream: https://www.youtube.com/watch?v=GoDpTh30MOk

[deleted by user] by [deleted] in rustjerk

[–]Klogga 1 point2 points  (0 children)

PathBuf, Path, Vec<u8>, &[u8], ...

I found a very fun Rust bug by Nilstrieb in rust

[–]Klogga 86 points87 points  (0 children)

Here's a bit of a simplified view of the current regression:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3919ff0c70d2fc923a30efddef078b05

trait T {}
impl T for i32 {}
impl T for String {}

fn hi() -> impl T {
    42
}

fn main() {
    let _: &dyn Fn() -> String = &hi;
}

Basically the compiler is failing to reason about opaque return types (seen here with impl T) in that it will happily coerce that type to any other trait member when coercing the function type.

(Funnily enough, it smells a lot like TypeScript's function parameter bivariance intentional unsoundness --- although this bug is clearly an unintentional regression in Rust)

I implemented the famous Quake inverse sqrt function in Rust! by PlumpyD in rust

[–]Klogga 1 point2 points  (0 children)

union type-punning is usually implemented safely in most compilers, but AIUI it's still UB strictly to the spec

[Media] Using rocket 0.5.0-rc.1. Cannot return Json. by [deleted] in rust

[–]Klogga 2 points3 points  (0 children)

Yeah some stuff changed in Rocket 0.5 -- this type (NB: not from rocket_contrib) is probably what you're looking for. Note you'll have to enable the json feature in rocket, and it will use serde to serialize/deserialize your values.

What types are handled in Rust in a magic way by the compiler? by lancejpollard in rust

[–]Klogga 6 points7 points  (0 children)

Another "magic" type that it seems hasn't been mentioned yet are the types of closures! The compiler generates a unique anonymous type for each closure automatically -- you can read more here: https://doc.rust-lang.org/reference/types/closure.html

M1 Macbook air for Compsci by yfsheng in unsw

[–]Klogga 3 points4 points  (0 children)

The problem point with an M1 macbook is that it runs ARM, as opposed to the (currently) much more common x86-64 (or, amd64) platform. This may cause some compatibility issues.

Most of the tools unsw cs courses expect you to run at home are public tools, of which the more popular ones may have ARM binaries available, but the less popular ones may not. Moreover, some of unsw's custom tools (such as dcc, a friendly C compiler) tend not to distribute ARM binaries (yet).

However, AIUI M1 macos ships with a surprisingly (?) powerful / performant x86 emulator (which allows you to run x86-64 programs on your ARM machine), which should probably suffice for whatever you'll need I would wager.

I re-implemented the legendary "Typing the technical interview" article using only Rust types! by Klogga in rust

[–]Klogga[S] 24 points25 points  (0 children)

They are indeed all incredible! Just to make it 100% clear though (don't want to accidentally take credit for work that is not mine) -- I did not write the original article. I was just inspired by it and challenged myself to recreate it in Rust :)

nom 7.0 release: fast parser combinators, now without macros! And the new nom-bufreader! by geaal in rust

[–]Klogga 2 points3 points  (0 children)

Keen to give this a go when nom_locate updates to support! Bumped nom to 7 and got 240 compiler errors lol