Commit Graph

129 Commits

Author SHA1 Message Date
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