Skip to content

Port "random" builtin to rust#9593

Merged
faho merged 3 commits into
fish-shell:masterfrom
faho:riir-random
Feb 19, 2023
Merged

Port "random" builtin to rust#9593
faho merged 3 commits into
fish-shell:masterfrom
faho:riir-random

Conversation

@faho

@faho faho commented Feb 19, 2023

Copy link
Copy Markdown
Member

Description

Repeat of #9585

This ports the "random" builtin, that is the builtin called random and not just any random builtin, to rust.

The remaining issue here is some weirdness with how the C++ does the numbers - the tests explicitly exercise that you can declare random numbers from the smallest signed long long to the largest with a step size of the difference between the two, and I haven't been able to make that work. This allows the weird signed 64-bit number + an unsigned 64 bit step logic, by doing all the math in 128-bit space.

Also the error messages are a bit of a mess in the C++ - a step of "-1" is "invalid integer", but a step of "0" is "STEP must be a non-negative integer" for some reason?

This uses a new crate, rand, which is what seemed to be the recommended course if you needed a seedable RNG, which we do (random 12 seeds the RNG with "12").

Also I needed some way to get wcstoi to tell me it hasn't read the entire token, so I picked a brute-force approach. I would assume there's a more elegant approach?

This represents about 98% 99% of all the rust code I've ever written, so let's see how much I missed!

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst

*No* idea if this is the idiomatic thing to do
@faho faho added this to the fish 3.7.0 milestone Feb 19, 2023

@Xiretza Xiretza left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've found an acceptable solution to the integer width problem.

Please also run rustfmt (cargo fmt).

Comment thread fish-rust/src/builtins/random.rs Outdated
Comment thread fish-rust/src/builtins/random.rs Outdated
Comment thread fish-rust/src/builtins/random.rs Outdated
Comment thread fish-rust/src/builtins/shared.rs
Comment thread fish-rust/src/builtins/mod.rs
Comment thread fish-rust/src/builtins/random.rs Outdated
@Xiretza

Xiretza commented Feb 19, 2023

Copy link
Copy Markdown
Contributor

I hope nobody relies on seeded random values to be stable across fish versions.

@faho

faho commented Feb 19, 2023

Copy link
Copy Markdown
Member Author

I hope nobody relies on seeded random values to be stable across fish versions.

I do not believe it is possible to translate this while keeping the values stable, especially because I don't think the values are stable across C++ implementations? So we couldn't even port what that does.

Truth be told I would be surprised if anyone actually used the seeding functionality.

So this is simply one change we're gonna have to allow.

faho and others added 2 commits February 19, 2023 20:23
Turns out we can do it without switching to 128-bit wide numbers.

Co-authored-by: Xiretza <xiretza@xiretza.xyz>
@faho faho merged commit f01a5d2 into fish-shell:master Feb 19, 2023
@faho faho deleted the riir-random branch June 3, 2023 12:04
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants