These are chat archives for rust-lang/rust

13th
Jan 2019
SEGFAULT
@IGBC
Jan 13 00:02

Hi all I have a random problem:

I have a unsigned int (u32) that breaks down into multiple fields of data (Its actually a CPU instruction, I am extracting data from) is there a way I can write a data structure in rust to expose the fields in my u32?

Denis Lisov
@tanriol
Jan 13 00:16
Normally one uses methods for that.
Alexander Ronald Altman
@pthariensflame
Jan 13 00:18
@IGBC What @tanriol said. The about-to-be-stabilized u32::to_be_bytes function might be helpful for that, if using Rust beta is temporarily acceptable.
SEGFAULT
@IGBC
Jan 13 00:44
@tanriol how would I structure methods for that?
I'm emulating a cpu I don't want to be doing too many bit shifts to decode the instructions.
Denis Lisov
@tanriol
Jan 13 00:49
What does the instruction set look like?
SEGFAULT
@IGBC
Jan 13 00:54
MIPS.
But modded a bit
Ichoran
@Ichoran
Jan 13 00:55
Bit shifts tend to be really fast. I'd just use them and refactor later if it turns out that there exists a faster way (e.g. interpreting the u32 as bytes).
Denis Lisov
@tanriol
Jan 13 00:57
Makes sense. I'd probably try masking out the opcode instead of shifting. If you need more performance, you'd probably look into building a JIT of some kind anyway.
SEGFAULT
@IGBC
Jan 13 00:58
It's for a game. It don't gotta go fast. No one is gonna be running an OS on this.
Ichoran
@Ichoran
Jan 13 00:58
Then just write it so it's correct :) Are you worried about getting the right bits out and into the u32?
Denis Lisov
@tanriol
Jan 13 00:59
Ok, then just write with shifts/masks for now and don't optimize until you find the real bottlenecks :-)
SEGFAULT
@IGBC
Jan 13 00:59
Will do. Thanks guys!
@tanriol i hope I don't have to write a jit. I'm not that smart.
Ichoran
@Ichoran
Jan 13 01:01
If you need it to go faster than about 50x slower than the original, you might (depending on details). Getting an interpreter no worse than 50x slower is usually pretty easy.
Denis Lisov
@tanriol
Jan 13 01:02
And yes... if your "for a game" means that you're writing a game with a computer inside it... don't underestimate the gamers. Pretty sure someone will run an OS in there :-)
SEGFAULT
@IGBC
Jan 13 01:02
A typical x86 runs at 2ghz.
Ichoran
@Ichoran
Jan 13 01:03
Maybe we can get it as a rust cross-compilation target :joy:
SEGFAULT
@IGBC
Jan 13 01:03
If I can hit 1mhz that will already meet my needs
Ichoran
@Ichoran
Jan 13 01:03
Yeah, you should be fine. It's always possible to mess things up and be super-slow by choosing inappropriate algorithms, but taking appropriate care you shouldn't have any trouble with speed.
SEGFAULT
@IGBC
Jan 13 01:03
So I can spend 2000 cycles doing an instruction,
🤔 I think that'll be tin
*fine
Well I hope at least one of my fans will try to write a
C rust compiler for it
But good luck making an OS work with 8 registers and no mmu
Ryan Levick
@rylev
Jan 13 11:15
I'd like to iterate over a stack allocated array (I basically have 4 stack allocate variables I'd like to perform the same operations on). I thought I could just put them into an an array [item0, item1, item2, item3] and call into_iter on them to get an owned iterator, but this gives me a an iterator over references. Any workarounds?
Tim Robinson
@1tgr
Jan 13 11:24
An iterator over references shouldn't be a problem; you can dereference each item if needed
Array iterators do give references, as items aren't moved out of the array during iteration (unlike Vec)
Ryan Levick
@rylev
Jan 13 11:27
Ah makes sense. Thanks!
Ravi Teja
@tekjar
Jan 13 14:13
What does it take to write android HAL processes in rust?
Farzeen
@happycoder97
Jan 13 17:02
In C and C++, I usually declare functions at the top of my file, to provide a birds eye view of the program when I comeback to it later.
Is there any technique/pattern that can help me do this in Rust?
Joshua Barretto
@zesterer
Jan 13 17:03
The best advice I can give is to keep your files/modules small. If you're going over 500 lines in one file, you know it's time to make another.
Farzeen
@happycoder97
Jan 13 17:04
Okay
Although this "summary" at the top is helpful even for files less than 500 lines
Tim Robinson
@1tgr
Jan 13 17:06
There's not really an equivalent in Rust as declaration and definitions are not separate
You could use Rustdoc, or an IDE that gives you an outline of the functions in the program
Farzeen
@happycoder97
Jan 13 17:06
Okay
moofone
@moofone
Jan 13 22:52
Hi all. What's a good mysql lib for rust. I don't need orm. https://docs.rs/mysql/15.0.0/mysql/ ?