Commit Graph

336 Commits

Author SHA1 Message Date
Johannes Altmanninger
a0eed3760e Cargo.toml: sort dependencies 2023-03-26 17:24:45 +02:00
Johannes Altmanninger
eb377d3c65 common.rs: implement Default for EscapeFlags 2023-03-26 17:17:37 +02:00
Johannes Altmanninger
981e470a2e common.rs: use bitflags for escape flags
See this discussion:
https://github.com/fish-shell/fish-shell/pull/9636#discussion_r1125640395
2023-03-26 17:17:37 +02:00
Johannes Altmanninger
b64c3eb79b termsize.rs: export Termsize 2023-03-26 17:17:37 +02:00
Johannes Altmanninger
d073b7140b lib.rs: sort modules 2023-03-26 17:17:37 +02:00
Johannes Altmanninger
16fa942074 parse_constants.rs: stop decoding UTF-8 when parsing keywords
Unfortunately we cannot use wide string literals in match statements
(not sure if there's an easy fix).
Because of this, I converted the input to UTF-8 so we could use the match
statement. This conversion is confusing, let's skip it.
2023-03-26 17:17:37 +02:00
ridiculousfish
b8189da011 Use the rust-pcre2 crate for regex
This adds support for our (forked) rust-pcre2 crate.
2023-03-25 17:01:50 -07:00
Mahmoud Al-Qudsi
fb74f77c86 Use bsd feature for signals
Signals present in 4.4BSD can be assumed present on all modern BSD derivatives.
2023-03-20 20:28:25 -05:00
Mahmoud Al-Qudsi
f2cf54608d Migrate existing rust code to Signal type
Everything but signal handlers has been changed to use `Signal` instead of
`c_int` or `i32` signal values.

Event handlers are using `usize` to match C++, at least for now.
2023-03-20 16:17:28 -05:00
Mahmoud Al-Qudsi
1f4c233dfb Add Signal newtype
Signal is a newtype around NonZeroI32. We could use NonZeroU8 since all signal
values comfortably fit, but using i32 lets us avoid a fallible attempt at
narrowing values returned from the system as integers to the narrower u8 type.

Known signals are explicitly defined as constants and can be matched against
with equality or with pattern matching in a `match` block. Unknown signal values
are passed-through without causing any issues.

We're using per-OS targeting to enable certain libc SIGXXX values - we could
change this to dynamically detecting what's available in build.rs but then it
might not match what libc exposes, still giving us build failures.
2023-03-20 16:17:28 -05:00
AsukaMinato
2e66bb19da use $( ... )* syntax 2023-03-20 11:20:12 -07:00
ridiculousfish
732f7284d4 Adopt the new termsize
This eliminates the C++ version.
2023-03-19 16:13:41 -07:00
ridiculousfish
6ec35ce182 Reimplement termsize in Rust
This is not yet adopted by fish.
2023-03-19 16:13:41 -07:00
Mahmoud Al-Qudsi
3fab931e86 Fix build.rs formatting and prep it for further feature detections 2023-03-19 18:12:50 -05:00
ridiculousfish
99c6c76c5e Add the category name back to FLOG output in Rust
This went missing.
2023-03-19 16:04:57 -07:00
Mahmoud Al-Qudsi
34a4c7de7f Add BSD feature
This should be used in lieu of manually targeting individual operating systems
when using features shared by all BSD families.

e.g. instead of

   #[cfg(any(target_os = "freebsd", target_os = "dragonflybsd", ...))]
   fn foo() { }

you would use

    #[cfg(feature = "bsd")]
    fn foo() { }

This feature is automatically detected at build-time (see build.rs changes) and
should *not* be enabled manually. Additionally, this feature may not be used to
conditionally require any other dependency, as that isn't supported for
auto-enabled features.
2023-03-19 17:55:22 -05:00
ridiculousfish
57f4571a01 Rewrite wait handles and wait handle store in Rust 2023-03-18 18:53:04 -07:00
AsukaMinato
14d6b1c3de Simplify Default impl for ParseError
By implementing `Default` for `ParseErrorCode`, `ParseError` can just
`#[derive(Default)]` instead.

Closes #9637.
2023-03-17 19:59:52 -05:00
Xiretza
b39715434b ScopeGuard: remove memory leak
Calling ScopeGuard::rollback() would leak the `on_drop` callable; this is
a problem for Box<dyn FnOnce> or closures containing Drop data.
2023-03-13 11:54:05 -05:00
ridiculousfish
dea18b34aa Add tests for normalize_path and fix some bugs 2023-03-12 19:50:35 -07:00
ridiculousfish
33fd679f68 Use char_at instead of to_char_slice() 2023-03-12 19:50:35 -07:00
ridiculousfish
f54a45d09c Add missing builtin_print_help in realpath
This got dropped in the port.
2023-03-12 19:50:35 -07:00
Victor Song
88e0c2137a Added constants for expansions 2023-03-12 19:50:35 -07:00
Victor Song
80c8bc75e6 Switch to errno crate 2023-03-12 19:50:35 -07:00
Victor Song
3dfc9082e6 Use std::io::Error::last_os_error() for errno 2023-03-12 19:50:35 -07:00
Victor Song
ca494778e4 builtins: Port realpath to Rust 2023-03-12 19:50:35 -07:00
Mahmoud Al-Qudsi
47b4e3d067 fixup! Switch signals from usize to i32
Just address two clippy lints that are fallout from changing the signal type.
There's no longer any need to convert these (which gets rid of an unwrap).
2023-03-12 21:38:24 -05:00
Mahmoud Al-Qudsi
4f30993dbb Use ScopeGuard to replace manually saved-and-restored variables 2023-03-12 21:32:35 -05:00
Mahmoud Al-Qudsi
11766cf56f Add a proper rust ScopeGuard
Due to limitations imposed by the borrow checker, there are very few places
where we will be able to use the `ScopedPush` class ported over from the C++
codebase (once you capture the value w/ a `ScopedPush` you can't access the
value - or the mutable reference you used to reach it! - until the `ScopedPush`
object goes out of scope).

This alternative requires binding the previous values to a variable and manually
restoring them in the callback passed to the `ScopeGuard` constructor, but will
work with rust's borrow and `&mut` paradigm.
2023-03-12 21:32:35 -05:00
Victor Song
06547aef54 Detect rust-analyzer in build script to enable autocxx completions
Currently the `autocxx` generated code does not produce any code intelligence
because `rust-analyzer` can't find the generated code since it's not in the
workspace. Here, we detect `rust-analyzer` by checking for a `RUSTC_WRAPPER`
environment variable containing `rust-analyzer` and changing (or avoid changing)
the output directory accordingly.

Closes #9654.
2023-03-12 21:31:28 -05:00
ridiculousfish
409bf2995d Switch signals from usize to i32
This eliminates some conversions.
2023-03-12 17:08:35 -07:00
ridiculousfish
161734f310 Remove bitset module
This was added to support signals; however we are unlikely to use this
for anything else. Remove it; just use a u64 to report signals that have
been set.
2023-03-12 16:58:22 -07:00
Mahmoud Al-Qudsi
8e9dc74a02 Simplify EventType matching slightly 2023-03-12 16:24:04 -05:00
Victor Song
77fe9933e2 builtins: Rewrite pwd in Rust
Closes #9625.
2023-03-12 15:18:15 -05:00
Mahmoud Al-Qudsi
6809a8dfbc Use a bit set for pending signals
This optimizes over both the rust rewrite and the original C++ code. The rust
rewrite saw `std::bitset` replaced with `[bool; 65]` which could result in a
lot of memory copy bandwidth each time we checked for and received no signals.
The original C++ code would iterate over all signal slots to see if any were
set. The code now returns a single u64 and only checks slots that are known to
have signals via an intelligent `Iterator` impl.
2023-03-12 14:55:50 -05:00
Xiretza
9ac6cbefb1 Port event.cpp to rust
Port src/event.cpp to fish-rust/event.rs and some needed functions.

Co-authored-by: Mahmoud Al-Qudsi <mqudsi@neosmart.net>
2023-03-12 14:55:50 -05:00
Mahmoud Al-Qudsi
c8d2f7a0da Add trait to convert FFI reference to &wstr
You can now use a reference to CxxWString or an allocated UniquePtr<CxxWString>
to get an &wstr temporary to use without having to allocate again (e.g. via
`from_ffi()`).
2023-03-12 14:55:50 -05:00
ridiculousfish
5197bf75cd Point fish autocxx and similar dependencies at new fish-shell location
These crates have been moved into fish-shell org; update Cargo.toml to
reflect that.
2023-03-09 21:01:49 -08:00
Johannes Altmanninger
c6756e9324 Canonicalize some wide string imports
wchar.rs should not import let alone reexport FFI strings.
Stop re-exporting utf32str! because we use L! instead.

In wchar_ffi.rs, stop re-exporting cxx::CxxWString because that hasn't
seen adoption.

I think we should use re-exports only for aliases like "wstr" or for aliases
into internal modules.
So I'd probably remove `pub use wchar_ffi::wcharz_t = crate::ffi::wcharz_t`
as well.
2023-03-05 10:32:20 +01:00
Johannes Altmanninger
e6994ea3ac Remove obsolete clippy suppression
This type has been extracted to an alias, so it is okay now.
2023-03-05 10:32:20 +01:00
Mahmoud Al-Qudsi
d839fea748 Silence some more clippy lints
bool_assert_comparison is stupid, the reason they give is "it's shorter". Well,
`assert!(!foo)` is nowhere near as readable as `assert_eq!(foo, false)` because
of the ! noise from the macro.

Uninlined format args is a stupid lint that Rust actually walked back when they
made it an official warning because you still have to use a mix of inlined and
un-inlined format args (the latter of which won't complain) since only idents
can be inlined.
2023-03-05 00:54:17 -06:00
Mahmoud Al-Qudsi
4828346f8b Implement and use Read and Write traits for AutoCloseFd
This lets us use any std::io functions that build on top of these, such as
`write_all()` in place of our own `write_loop()`.
2023-03-05 00:33:54 -06:00
Mahmoud Al-Qudsi
455b744bca Port fd_monitor tests to rust
This shows some of the ugliness of the rust borrow checker when it comes to
safely implementing any sort of recursive access and the need to be overly
explicit about which types are actually used across threads and which aren't.

We're forced to use an `Arc` for `ItemMaker` (née `item_maker_t`) because
there's no other way to make it clear that its lifetime will last longer than
the FdMonitor's. But once we've created an `Arc<T>` we can't call
`Arc::get_mut()` to get an `&mut T` once we've created even a single weak
reference to the Arc (because that weak ref could be upgraded to a strong ref at
any time). This means we need to finish configuring any non-atomic properties
(such as `ItemMaker::always_exit`) before we initialize the callback (which
needs an `Arc<ItemMaker>` to do its thing).

Because rust doesn't like self-referential types and because of the fact that we
now need to create both the `ItemMaker` and the `FdMonitorItem` separately
before we set the callback (at which point it becomes impossible to get a
mutable reference to the `ItemMaker`), `ItemMaker::item` is dropped from the
struct and we instead have the "constructor" for `ItemMaker` take a reference to
an `FdMonitor` instance and directly add itself to the monitor's set, meaning we
don't need to move the item out of the `ItemMaker` in order to add it to the
`FdMonitor` set later.
2023-03-05 00:33:53 -06:00
Mahmoud Al-Qudsi
83a220a532 Make fd_monitor types useable from native code
We were only using their ffi implementations which are automatically
exported/public, but the actual functions we would need if we were to use
FdMonitor and co. in native rust code were either private or missing convenient
wrappers.
2023-03-05 00:23:01 -06:00
Mahmoud Al-Qudsi
78a78a834c Port read_loop() and write_loop() to rust
The existing code is kept, but a rusty version of these functions is added for
code that needs them.

These should only be temporarily used when porting 1-to-1 from C++; we should
use the std library's `read()` and `write_all()` methods instead in the future.
2023-03-05 00:22:56 -06:00
Mahmoud Al-Qudsi
f2f7d1d183 Simplify assert_sorted_by_name! macro
By extracting the equivalent of i32::cmp() into its own const function,
it becomes a lot easier to see what is happening and the logic can be
more direct.
2023-03-04 17:05:11 -06:00
Johannes Altmanninger
2c331e9c69 Implement more bitwise operation for parser bitfields
These will be used in the parser.

Maybe this type should be a struct with boolean fields. The current way has
the upside that the usage is exactly the same as in C++.
2023-03-04 22:24:22 +01:00
Johannes Altmanninger
b92313b79d Allow using wgettext_fmt without comma from macros
Otherwise we'd get this error when using it from another macro

        Some(wgettext_fmt!($fmt $(, $args)*))
                               ^ missing tokens in macro arguments
2023-03-04 22:24:22 +01:00
Johannes Altmanninger
7ec27617ae Support widestring macro on non-literal strings
This enables usage in macros like

        L!(stringify!($snake_case_name))

in the upcoming AST port.
2023-03-04 22:24:22 +01:00
Johannes Altmanninger
be89793669 Fix buffer overflow accessing error source in ParseError::describe()
For some reason this error is triggered by tests after the Rust port of
ast.cpp. Might want to get to the bottom of this but moving it back
to match the original C++ logic fixes it.
2023-03-04 22:24:22 +01:00
Johannes Altmanninger
386f952c53 Implement constructors for some parser types 2023-03-04 22:24:22 +01:00
Johannes Altmanninger
913eeffa7e Derive Copy for some parser types 2023-03-04 22:24:22 +01:00
Johannes Altmanninger
bb1c64b202 Make some parser types public 2023-03-04 22:24:22 +01:00
Johannes Altmanninger
d0bda9893b Silence -Wcomment warnings in cxx compiler runs
This is one of the few warnings we disable due to false positives.  Let's also
disable it in the preprocessing steps needed for the Rust build.

Other warnings we ignore are -Wno-address -Wunused-local-typedefs and
-Wunused-macros. I didn't add them here because I don't expect that they
will be triggered by the headers we give to cxx.
2023-03-04 22:24:22 +01:00
Xiretza
8427e05bf7 Move escape_string tests to Rust
This way, both the Rust FFI wrapper and the actual C++ implementation are
tested.
2023-03-04 12:42:06 -08:00
Xiretza
7585ddf926 Port color.cpp to Rust 2023-03-04 11:46:46 -08:00
Xiretza
a23de237a6 Port ASSERT_SORTED_BY_NAME to Rust 2023-03-04 11:46:46 -08:00
ridiculousfish
a3970c1661 Improve FLOG output
Prior to this fix, the Rust FLOG output was regressed from C++, because
it put quotes around strings. However if we used Display, we would fail
to FLOG non-display types like ThreadIDs.

There is apparently no way in Rust to write a function which formats a
value preferentially using Display, falling back to Debug.

Fix this by introducing two new traits, FloggableDisplay and
FloggableDebug. FloggableDisplay is implemented for all Display types,
and FloggableDebug can be "opted into" for any Debug type:

    impl FloggableDebug for MyType {}

Both traits have a 'to_flog_str' function. FLOG brings them both into
scope, and Rust figures out which 'to_flog_str' gets called.
2023-03-04 11:35:21 -08:00
Clemens Wasser
17c1fa9d64
Port bg builtin to Rust (#9621)
* bg: Port bg builtin to Rust
2023-02-28 16:42:12 -06:00
Victor Song
c7ea768a74
Rewrite wrealpath from wutil in Rust (#9613)
* wutil: Rewrite `wrealpath` in Rust

* Reduce use of FFI types in `wrealpath`

* Addressed PR comments regarding allocation

* Replace let binding assignment with regular comparison
2023-02-26 20:13:40 -07:00
Clemens Wasser
6f5be9bae4 block: Port block builtin to Rust
Closes #9612.
2023-02-26 14:16:55 -06:00
Xiretza
dff7db2f16
Run rustfmt and clippy in CI (#9616)
* Add machine-readable MSRV to Cargo.toml
* Fix clippy warnings
* CI: add rustfmt and clippy checks
2023-02-26 13:20:20 -06:00
Mahmoud Al-Qudsi
562eeac43e
Port job_group to rust (#9608)
More ugliness with types that cxx bridge can't recognize as being POD. Using
pointers to get/set `termios` values with an assert to make sure we're using
identical definitions on both sides (in cpp from the system headers and in rust
from the libc crate as exported).

I don't know why cxx bridge doesn't allow `SharedPtr<OpaqueRustType>` but we can
work around it in C++ by converting a `Box<T>` to a `shared_ptr<T>` then convert
it back when it needs to be destructed. I can't find a clean way of doing it
from the cxx bridge wrapper so for now it needs to be done manually in the C++
code.

Types/values that are drop-in ready over ffi are renamed to match the old cpp
names but for types that now differ due to ffi difficulties I've left the `_ffi`
in the function names to indicate that this isn't the "correct" way of using the
types/methods.
2023-02-25 16:42:45 -06:00
Neeraj Jaiswal
f52569a800 abbr: port abbreviation and abbr builtin to rust 2023-02-25 12:24:58 +01:00
Neeraj Jaiswal
b0ed37c2e0 format: support whitespace padding in str formatting 2023-02-25 12:24:58 +01:00
Neeraj Jaiswal
e384e63b24 re: port regex make anchored to rust and helper ffi funtions for regex 2023-02-25 12:24:57 +01:00
Neeraj Jaiswal
6851d52924 env: port env constants to rust 2023-02-25 12:24:32 +01:00
Neeraj Jaiswal
7bab4c4dda common: pass c_str in ffi escape string 2023-02-25 12:24:32 +01:00
Johannes Altmanninger
5394ca1f96 Address clippy lints 2023-02-25 12:24:25 +01:00
Johannes Altmanninger
0d6b53bc3e Address clippy lints
We want to keep the cast because tv_sec is not always 64 bits, see b5ff175b4
(Fix timer.rs cross-platform compilation, 2023-02-14).
It would be nice to avoid the clippy exemption, perhaps using something like

    #[cfg(target_pointer_width = "32")]
    let seconds = val.tv_sec as i64;
    #[cfg(not(target_pointer_width = "32"))]
    let seconds = val.tv_sec;

but I'm not sure if "target_pointer_width" is the right criteria.
2023-02-25 12:24:25 +01:00
Johannes Altmanninger
30d40c1d49 ffi.rs: sort includes in include_cpp
If we sort includes as we add them instead of adding them at the end, we'll
have fewer conflicts.
2023-02-25 12:24:25 +01:00
Neeraj Jaiswal
3b60bc1de0 contains: port contains builtin to rust 2023-02-22 18:32:27 +01:00
Mahmoud Al-Qudsi
aca7dedf33 Fix Tokenizer::parse_fd() on x86
Upsizing to `usize` from `i32` doesn't work if `usize` is only 32-bits.
I changed the code to use the `FromStr` impl on `i32`, but we could have also
just used `u64` instead of `i32`.

Also, we should get in the habit of using the appropriate type aliases where
possible (`i32` should be `RawFd`).
2023-02-20 13:41:11 -06:00
Mahmoud Al-Qudsi
e616de544e Enable rust overflow checks in release mode, at least for now
We want to try and catch as much unexpected/non-deterministic behavior as we
can. We could run the CI explicitly in debug mode, but I think it makes sense to
always have overflow checks on in both debug/release modes everywhere, at least
for the duration of the codebase transition.
2023-02-20 13:11:29 -06:00
Fabian Boehm
e3b04118b1 Revert "random: Do math as unsigned"
This reverts commit 0902e29f49.

Just doesn't work - overflows.
2023-02-20 19:56:34 +01:00
Fabian Boehm
0902e29f49 random: Do math as unsigned
Hahah bits go brrrr
2023-02-20 19:39:55 +01:00
Xiretza
77a474ee37 Move POD components of library_data_t to separate struct
This allows them to be accessed as regular fields from Rust, rather than having
to create setter/getter methods for each of them.
2023-02-20 11:32:12 +01:00
Mahmoud Al-Qudsi
59fe124c40 builtins/random: Don't lock the mutex unnecessarily
The mutex was being locked from the very start, before it was needed and
possibly before it would be needed.

Also rename the static global to stick to rust naming conventions.

Note that `once_cell::sync::Lazy<T>` actually internally uses its own lock
around the value, but in this case it's insufficient because `SmallRng` doesn't
implement `SeedableRng` so we can't reseed it with only an `&mut` reference and
must instead replace its value.

We probably *could* still use `Lazy<SmallRng>` directly and then rely on
`std::mem::swap()` to replace the contents of the shared global static without
reassigning the variable directly with a new `SmallRng` instance, but I'm not
sure that's a great idea. This is just a built-in, there's no real harm in
locking twice (especially while fish remains essentially single-threaded).
2023-02-19 16:54:50 -06:00
Mahmoud Al-Qudsi
51eb5168e8 builtins/random: Fix stale comments and use explicit output type
The old comments about using i128 logic were still there even though we are no
longer using that approach and the output type was very much misleadingly a u64
printed to the console (but via `%d` so it was ultimately shown as an i64). Be
explicit about the resulting being a valid i64 value before passing it to the
sprintf!() macro.

Also add comments about the safety of the final `unwrap()` operation.
2023-02-19 16:54:50 -06:00
Mahmoud Al-Qudsi
05265e7d90 Port (and use) ASSERT_IS_BACKGROUND_THREAD/ASSERT_IS_MAIN_THREAD
Rust doesn't have __FUNCTION__ or __func__ (though you can hack around it with a
proc macro, but that will require a separate crate and slowing down compilation
times with heavy proc macro dependencies), so these are just regular functions
(at least for now). Rust's default stack trace on panic (even in release mode)
should be enough (and the functions themselves are inlined so the calling
function should be the second frame from the top, after the #[cold] panic
functions).
2023-02-19 16:54:50 -06:00
Mahmoud Al-Qudsi
452cd90c6c Add test asserting std::thread's behavior matches pthread's on *nix
This is to allow us to verify some implementation details that aren't explicitly
documented in the rust standard library's documentation.

std::thread uses `pthread_create()` underneath the hood on *nix platforms, so
this *should* merely be a formality.
2023-02-19 15:42:07 -06:00
Mahmoud Al-Qudsi
aaf2d1c19d Use * const u8 instead of * const c_void
The way cxx bridge works, it doesn't recognize any types from another module as
being shared cxx bridge types with generations native to both C++ and Rust,
meaning every module that was going to use function pointers would have to
define its own `c_void` type (because cxx bridge doesn't recognize any of
libc::c_void, std::ffi::c_void, or autocxx::c_void).

FFI on other platforms has long used the equivalent of `uint8_t *` as an
alternative to `void *` for code where `void` was not available or was
undesirable for some reason. We can join the club - this way we can always use
`* {const|mut} u8` in our rust code and `uint8_t *` in our C++ code to pass
around parameters or values over the C abi.
2023-02-19 15:42:07 -06:00
Mahmoud Al-Qudsi
8deaede6c7 Patch a few minor issues in fd_monitor
These differ from the C++ code and are being committed separately.
2023-02-19 15:42:07 -06:00
Mahmoud Al-Qudsi
ce559bc20e Port fd_monitor (and its needed components)
I needed to rename some types already ported to rust so they don't clash with
their still-extant cpp counterparts. Helper ffi functions added to avoid needing
to dynamically allocate an FdMonitorItem for every fd (we use dozens per basic
prompt).

I ported some functions from cpp to rust that are used only in the backend but
without removing their existing cpp counterparts so cpp code can continue to use
their version of them (`wperror` and `make_detached_pthread`).

I ran into issues porting line-by-line logic because rust inverts the behavior
of `std::remove_if(..)` by making it (basically) `Vec::retain_if(..)` so I
replaced bools with an explict enum to make everything clearer.

I'll port the cpp tests for this separately, for now they're using ffi.

Porting closures was ugly. It's nothing hard, but it's very ugly as now each
capturing lambda has been changed into an explicit struct that contains its
parameters (that needs to be dynamically allocated), a standalone callback
(member) function to replace the lambda contents, and a separate trampoline
function to call it from rust over the shared C abi (not really relevant to
x86_64 w/ its single calling convention but probably needed on other platforms).

I don't like that `fd_monitor.rs` has its own `c_void`. I couldn't find a way to
move that to `ffi.rs` but still get cxx bridge to consider it a shared POD.
Every time I moved it to a different module, it would consider it to be an
opaque rust type instead. I worry this means we're going to have multiple
`c_void1`, `c_void2`, etc. types as we continue to port code to use function
pointers.

Also, rust treats raw pointers as foreign so you can't do `impl Send for * const
Foo` even if `Foo` is from the same module. That necessitated a wrapper type
(`void_ptr`) that implements `Send` and `Sync` so we can move stuff between
threads.

The code in fd_monitor_t has been split into two objects, one that is used by
the caller and a separate one associated with the background thread (this is
made nice and clean by rust's ownership model). Objects not needed under the
lock (i.e. accessed by the background thread exclusively) were moved to the
separate `BackgroundFdMonitor` type.
2023-02-19 15:42:03 -06:00
Fabian Boehm
f01a5d2a1b random: Do it in 64-bits
Turns out we can do it without switching to 128-bit wide numbers.

Co-authored-by: Xiretza <xiretza@xiretza.xyz>
2023-02-19 21:01:46 +01:00
Fabian Boehm
4fd1458d85 Port random to rust 2023-02-19 21:01:46 +01:00
Fabian Boehm
bc7c29d597 wcstoi: Allow erroring out if there are chars left
*No* idea if this is the idiomatic thing to do
2023-02-19 21:01:46 +01:00
Xiretza
46aef09a90 Add more clippy exceptions for ffi module 2023-02-18 18:53:50 +01:00
Xiretza
698db6c2a7 builtins: make io_streams_t methods publicly accessible 2023-02-18 18:53:50 +01:00
Xiretza
71c2f08e5d printf: implement Printf for &WString 2023-02-18 18:53:50 +01:00
Xiretza
333056a9ec rust: add bindings for signal conversion functions 2023-02-18 18:53:50 +01:00
Xiretza
e6e866e455 Port escape_string() to Rust 2023-02-18 18:53:50 +01:00
Xiretza
15d4310ae9 Port scoped_push to Rust 2023-02-18 18:53:50 +01:00
Neeraj Jaiswal
844174367b wgetopt: fix long option match to always match prefix 2023-02-18 18:53:40 +01:00
Neeraj Jaiswal
1adfce18ee builtins: port return/exit to rust 2023-02-18 18:53:40 +01:00
Mahmoud Al-Qudsi
b5ff175b45 Fix timer.rs cross-platform compilation
* macOS does not have RUSAGE_THREAD
* tv_sec and tv_usec may be i32 instead of i64
2023-02-14 16:36:00 -06:00
Mahmoud Al-Qudsi
a1a8bc3d8d Port timer.cpp to rust 2023-02-14 15:54:18 -06:00
Xiretza
5a76c7d3b1 Port emit builtin to rust 2023-02-11 15:04:57 +01:00
Xiretza
3ed86fae1c Port parse_help_only_cmd_opts to Rust
This is duplicated for now, since a `&mut [&wstr]` can't be passed over FFI.
2023-02-11 15:04:57 +01:00
Johannes Altmanninger
39f3c894d7 Port tokenizer.cpp to Rust
In hindsight, I should probably have split this into three different commits.
2023-02-09 00:37:22 +01:00
Johannes Altmanninger
7f8d247211 Port parse_constants.h to Rust 2023-02-09 00:37:22 +01:00
Johannes Altmanninger
25816627de Port redirection.cpp to Rust 2023-02-09 00:37:22 +01:00
Johannes Altmanninger
4639f7ec40 Follow Rust naming convention for some types
But don't do it for enum variants just yet.
2023-02-08 21:49:54 +01:00
Johannes Altmanninger
958ad3a9e7 ffi.rs: silence warning about get_procs()
We should fix this warning eventually.  Silence it for now to make Clippy
pass without warnings, which makes it much more useful.

       Compiling fish-rust v0.1.0 (/home/johannes/git/fish-riir/fish-rust)
    error: mutable borrow from immutable input(s)
      --> src/ffi.rs:79:32
       |
    79 |     pub fn get_procs(&self) -> &mut [UniquePtr<process_t>] {
       |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |
    note: immutable borrow here
      --> src/ffi.rs:79:22
       |
    79 |     pub fn get_procs(&self) -> &mut [UniquePtr<process_t>] {
       |                      ^^^^^
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mut_from_ref
       = note: `#[deny(clippy::mut_from_ref)]` on by default

    error: could not compile `fish-rust` due to previous error
2023-02-08 21:49:41 +01:00
Johannes Altmanninger
29a2c4b718 gettext.rs: allow translating non-literal strings
A following commit will pass global string constants to the gettext macro.
This is not ideal because we might accidentally use the constants without
gettext (which we should never do). To fix that we might need to define a
macro per constant, or use a proc macro which is maybe not worth it.
2023-02-08 21:49:41 +01:00
Johannes Altmanninger
bfa94bfa7a Fix rustc warning about auto deref
warning: deref which would be done by auto-deref
      --> src/wchar_ffi.rs:81:5
       |
    81 |     &*EMPTY_WSTRING
       |     ^^^^^^^^^^^^^^^ help: try this: `&EMPTY_WSTRING`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref
       = note: `#[warn(clippy::explicit_auto_deref)]` on by default
2023-02-08 21:49:41 +01:00
Xiretza
a16e2ecb1b Port echo builtin to Rust 2023-02-07 22:25:47 +01:00
Xiretza
4b85c2f6db builtin: propagate status from Rust builtins
The return type of `builtin_run_rust()` reflects that of C++ builtins.
2023-02-07 22:25:47 +01:00
Mahmoud Al-Qudsi
0b160ebe71 Drop lazy_static from Cargo.toml
This should have been included as part of the previous commit, mea culpa.
2023-02-05 18:20:26 -06:00
Mahmoud Al-Qudsi
d7febd4f3e Use once_cell instead of lazy_static
lazy_static has better ergonomics at the call/access sites (it returns a
reference to the type directly, whereas with once_cell we get a static Lazy<T>
that we must dereference instead) but the once_cell api is slated for
integration into the standard library [0] and has been the "preferred" way to
declare static global variables w/ deferred initialization. It's also less
opaque and easier to comprehend how it works, I guess?

(Both `once_cell` and `lazy_static` are already in our dependency tree, so this
should have no detrimental effect on build times. It actually negligibly
*improves* build times by not using macros, reducing the amount of expansion the
compiler has to do by a miniscule amount.)

[0]: https://github.com/rust-lang/rust/issues/74465
2023-02-05 17:58:33 -06:00
Johannes Altmanninger
39c3faeaf4 gettext.rs: make trailing comma actually optional 2023-02-05 12:24:29 +01:00
Johannes Altmanninger
f167ec9063 clippy: silence manual_is_ascii_check
It's debatable whether is_ascii_digit() is better than (0..=9).contains().
(Probably we want to go with the mainstream Rust choice eventually.)
Let's disable the warning for now since it's not terribly important.
2023-02-05 12:24:29 +01:00
Johannes Altmanninger
c8bf2be408 wchar_ffi.rs: implement from_ffi() for more FFI strings 2023-02-05 12:22:42 +01:00
Johannes Altmanninger
dcca3cfe3c Prefer taking native Rust strings instead of wcharz_t
We should only be dealing with wcharz_t at the language boundary.
Rust callers should prefer the equivalent &wstr.
Since wcsfilecmp() is no longer exposed directly it can take &wstr only.
2023-02-05 12:22:42 +01:00
Johannes Altmanninger
a446a16471 flog.rs: use qualified name in FLOG! macro
Otherwise this macro fails when used in a context that doesn't import
this name.
2023-02-05 12:02:48 +01:00
Johannes Altmanninger
7347c90d1e builtins.rs: correct error message on unknown option 2023-02-05 12:02:48 +01:00
Johannes Altmanninger
476b12e06a util.rs: simplify wcsfilecmp a bit further 2023-02-05 12:02:48 +01:00
Johannes Altmanninger
ba1c5d495f util.rs: fix Yoda condition 2023-02-05 12:02:48 +01:00
Xiretza
8460b37b6a rust: util: use Ordering instead of integers 2023-02-05 11:57:25 +01:00
Xiretza
8b483735b4 rust: fix doc comments 2023-02-05 11:57:25 +01:00
Xiretza
cee13531e3 rust: silence warnings on auto-generated FFI bindings 2023-02-05 11:57:25 +01:00
Xiretza
cba03fc1e8 rust: remove unnecessary newline 2023-02-05 11:57:25 +01:00
Xiretza
35083c72ef rust: silence some clippy warnings 2023-02-05 11:57:25 +01:00
Xiretza
853649f8dc rust: fix issues reported by clippy 2023-02-05 11:57:25 +01:00
Johannes Altmanninger
83fd7ea7c4 Port future_feature_flags.cpp to Rust
This is early work but I guess there's no harm in pushing it?
Some thoughts on the conventions:

Types that live only inside Rust follow Rust naming convention
("FeatureMetadata").

Types that live on both sides of the language boundary follow the existing
naming ("feature_flag_t").
The alternative is to define a type alias ("using feature_flag_t =
rust::FeatureFlag") but that doesn't seem to be supported in "[cxx::bridge]"
blocks. We could put it in a header ("future_feature_flags.h").

"feature_metadata_t" is a variant of "FeatureMetadata" that can cross
the language boundary. This has the advantage that we can avoid tainting
"FeatureMetadata" with "CxxString" and such. This is an experimental approach,
probably not what we should do in general.
2023-02-03 18:55:06 +01:00
Johannes Altmanninger
517d53dc46 Port util.cpp to Rust
The original implementation without the test took me 3 hours (first time
seriously looking into this)

The functions take "wcharz_t" for smooth integration with existing C++ callers.
This is at the expense of Rust callers, which would prefer "&wstr".  Would be
nice to declare a function parameter that accepts both but I don't think
that really works since "wcharz_t" drops the lifetime annotation.
2023-02-03 18:55:06 +01:00
Johannes Altmanninger
44d75409d0 build.rs: re-run autocxx if any ffi module changed
I'm not 100% sure this is the right thing but it seems to fix a scenario
where a change to a Rust module was not propagated by "make".
2023-02-03 18:55:06 +01:00
Johannes Altmanninger
a502cb16c3 ffi.rs: prevent rustfmt from breaking "use" statements
rustfmt removes the "::" prefix from qualifiers. This breaks the build because
I think a later "pub use ffi::*" results in "std" being an ambiguous reference.
2023-02-03 18:55:05 +01:00
Mahmoud Al-Qudsi
60bd186e21 Fix linking errors under FreeBSD
The nix crate had all its default features enabled, which included features that
are not present under BSD. We should only enable the select subset of crate
features that we know are available cross-platform (or else use conditional
targeting in Cargo.toml to only enable Linux-only features when compiling for
Linux targets).

For now, it seems we can just use the nix crate with all features disabled as it
still builds under Linux and FreeBSD in this state.
2023-02-03 11:36:21 -06:00
Mahmoud Al-Qudsi
c18fb74fa8 Fix rust-invoked build of c/cpp sources under FreeBSD
Due to an upstream issue with cc-rs [0], the rust-generated C++ interface would
fail to compile. A PR has been opened to patch the issue upstream [1], but in
the meantime `Cargo.toml` has been patched to use a fork of cc-rs with the
relevant fixes.

[0]: https://github.com/rust-lang/cc-rs/issues/463
[1]: https://github.com/rust-lang/cc-rs/pull/785
2023-02-03 11:36:21 -06:00
ridiculousfish
76adfed0e7 Implement builtin_wait in Rust
This implements builtin_wait in Rust.
2023-02-02 19:34:48 -07:00
ridiculousfish
e674678ea4 Add a printf implementation
This allows using existing format strings.
The implementation is adapted from https://github.com/tjol/sprintf-rs
2023-02-02 19:34:48 -07:00
ridiculousfish
55f655f003 Add a gettext wrapper in Rust
This allows the wgettext! macro, which calls into C++.
2023-02-02 19:34:48 -07:00
ridiculousfish
681a165721 Add an FFI test facility
This allow testing Rust functions (from fish_tests.cpp) which need to
cross the FFI. See the example in smoke.rs.
2023-02-02 19:34:48 -07:00
ridiculousfish
096b254c4a Port fish_wcstoi to Rust
This adds an implementation of fish_wcstoi in Rust, mirroring the one in
fish. As Rust does not have a string to number which infers the radix
(i.e. looks for leading 0x or 0), we add that manually.
2023-02-02 19:34:48 -07:00
ridiculousfish
d843b67d2d Initial Rust commit 2023-02-02 19:34:47 -07:00