Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
gonadic io
Feb 16, 2011

>>=
is locking stdout for a long time like that an issue? I don't really know what its implications are tbh. I personally would be tempted to populate a vec then write out in one go after

Adbot
ADBOT LOVES YOU

crazypenguin
Mar 9, 2005
nothing witty here, move along
Locking stdout is purely a process-internal thing, not something with some kind of OS-level effect.

...if that's what you're wondering.

gonadic io
Feb 16, 2011

>>=
sure yeah, thanks

Ihmemies
Oct 6, 2012

gonadic io posted:

is locking stdout for a long time like that an issue? I don't really know what its implications are tbh. I personally would be tempted to populate a vec then write out in one go after

I tried that first, populate a vector with ints. Use println! and iterators to print it in one go. I could not get either print or println work fast enough. With n=200000 writing to stdout gave a 0,4s execution time. With any kind of print it was over 1s with my (lack of) skill.

gonadic io
Feb 16, 2011

>>=
You definitely want to lock rather than call print in a loop because each print call locks and then unlocks stdout anyway. I meant writing to a vec and then calling write on the vec as you did inside your inner loop. But it's probably a pretty moot point if locking stdout for your program is not an issue

Ihmemies
Oct 6, 2012

This is just an algorithm course, so nothing serious. I began learning rust two weeks ago, because I needed a compiler which babysits me.

We have 100 different algorithms to implement, and now after a soft start the tasks seem to get stricter. One thing I did not try out was storing the values to vector and then writing it instead of printing. Iterating a vector of 200k indexes adds an extra delay though, so just writing the data after each calculation is done seems to be faster.

gonadic io
Feb 16, 2011

>>=
being babysat by a compiler owns

Ihmemies
Oct 6, 2012

Before implementing a command line argument tool in "main" bin, I used a bin for each day in advent of code.

Now I wonder how can I run only tests for day_13.rs for example. They are in end of day_13.rs

Rust code:

#[cfg(test)]
mod tests {
    use crate::utils::{self, read_data_from_file};
    use super::*;   

    #[test]
    fn test_day13() {
        test_silver();
        test_gold();
    }  

    #[test]
    fn test_silver() {
        let test_data:Vec<String> = read_data_from_file("input/real/13.txt");
        assert_eq!(silver(&test_data), 27502);
    }

    #[test]
    fn test_gold() {
        let test_data:Vec<String> = read_data_from_file("input/real/13.txt");
        assert_eq!(gold(&test_data), 31947);
    }
}
cargo test test_day13 runs all the very non-existing tests from all of my binaries. Is there some way to configure it to run actually only the tests in day_13.rs ..?

It just spams my terminal with useless crap currently :I

E: Heh, the "Run Test" button above mod tests in day_13.rs in VSCode magically told me the correct command:

code:
cargo test --bin main -- day_13::tests
Now I just need to remember that!

Ihmemies fucked around with this message at 19:35 on Dec 13, 2023

gonadic io
Feb 16, 2011

>>=
there's also a -Z for inexact matching

bitprophet
Jul 22, 2004
Taco Defender
There’s also a half decent cargo-aoc plugin out there which greatly simplifies this stuff, if wrestling with a cli tool isn’t something you actively enjoy doing. (Its “generator” feature is flaky but also just not necessary.)

Ihmemies
Oct 6, 2012

bitprophet posted:

There’s also a half decent cargo-aoc plugin out there which greatly simplifies this stuff, if wrestling with a cli tool isn’t something you actively enjoy doing. (Its “generator” feature is flaky but also just not necessary.)

I run the code on my linux server, because setting up stuff there is so much easier. So program runs on VSCode's terminal window.

You mean this onehttps://crates.io/crates/cargo-aoc ? Hmm maybe I should test it, thanks. Although since I started learning rust like 2 weeks ago maybe it's good first to do things more manually.

bitprophet
Jul 22, 2004
Taco Defender

Ihmemies posted:

I run the code on my linux server, because setting up stuff there is so much easier. So program runs on VSCode's terminal window.

You mean this onehttps://crates.io/crates/cargo-aoc ? Hmm maybe I should test it, thanks. Although since I started learning rust like 2 weeks ago maybe it's good first to do things more manually.
Yup, that one. I’m relatively new myself and my take was that AoC was hard enough as it is, I didn’t need to add “learn how rust thinks about CLI parsing, then write my own logic around dispatching” to the list.

Granted, I then put AoC down a few days into it, but that wasn’t the plugin’s fault, just realizing AoC wasn’t a good use of my time right now. (Even those 4 days were a great boost to my Rust tho!)

gonadic io
Feb 16, 2011

>>=
my aoc hot tip is the include_str macro (or include_bytes when you want to shave some microseconds). gently caress reading from files at runtime

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

I have a binary file format that I'd like to parse into a Rust structure and I'm wondering if serde is the wrong crate for it. A few issues that I've run into:

1) There's padding/unused bytes in the file format and serde doesn't seem to have a way of annotating something to the effect of "for this next field, fseek two bytes ahead", nor does it seem to be obvious that serde would treat a PhantomData<u16> as padding. (Besides, manually inserting padding fields into the struct just so serde does the right thing feels a bit funny anyway.)

2) There are lots of bitfields in the file format that I'd like to expand as enums with two discriminants. The problem seems to be that serde (reasonably) doesn't expose consuming data smaller than a byte, nor does it let me read all the bitfields in a byte and then produce multiple types.

Seems like the only way to do this is to just implement the entirety of Deserialize on my structure and doing everything manually. (edit: or just read in a blob of [u8] and std::mem::transmute stuff out as I see fit, which I'm not opposed to, per se, but...)

If that's the best way, then fine, but I suspect either serde is the wrong crate or I'm approaching this wrong?

Dijkstracula fucked around with this message at 20:00 on Dec 16, 2023

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
Take a look at nom, perhaps?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

you could probably write a custom deserializer for that field and do the seeking you want, but I agree that nom is probably a better fit

Moonlit Knight
Nov 26, 2018
I think scroll is a popular crate for this kind of usecase. You might also want to have a look st zerocopy or watto (disclaimer: I worked on watto).

tinaun
Jun 9, 2011

                  tell me...
Serde is not a parsing library.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

tinaun posted:

Serde is not a parsing library.
Indeed, and I’m not in need of a parser here either. I reached to Serde first only because jonhoo made use of it in his BitTorrent client livestreams and it seemed like I was operating at a similar level of abstraction.

Thanks, all- sounds like nom is the thing I should use.

pseudorandom name
May 6, 2007

BitTorrent uses Bencode and Bencode already has a serde implementation.

It’s really easy to use a serialization library with a serialization format, especially when the work has already been done for you. Not so much for ad hoc bit twiddling.

Adbot
ADBOT LOVES YOU

Ihmemies
Oct 6, 2012

I am learning structs are quite handy. For a simple struct like this:

Rust code:
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct Coord {
    pub x: isize,
    pub y: isize,
}
You can implement features like this:



So it checks if the coord fits certain bounds. AoC has different sizes of maps and doing always the same bounds checking individually in different solutions starts to suck.

Then I realized you can write cool comments like that, so my IDE shows tooltips for my own functions, just like it does for STD's functions. Sweet..

AoC day 17 progress is not great, but not terrible. My utils.rs is growing in functionality at least.

First I used usize as coords, then I thought about what if I want to take a cruise around origin.. and realized I need to support negative coordinates too.

Ihmemies fucked around with this message at 17:29 on Dec 17, 2023

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply