From dcca3cfe3c84a85c04608046a318d7e6513c3332 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 4 Feb 2023 12:29:06 +0100 Subject: [PATCH] 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. --- fish-rust/src/future_feature_flags.rs | 8 +++----- fish-rust/src/util.rs | 20 +++++--------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/fish-rust/src/future_feature_flags.rs b/fish-rust/src/future_feature_flags.rs index 3755bbfa4..3b1d86e42 100644 --- a/fish-rust/src/future_feature_flags.rs +++ b/fish-rust/src/future_feature_flags.rs @@ -166,7 +166,7 @@ impl features_t { /// The special group name "all" may be used for those who like to live on the edge. /// Unknown features are silently ignored. #[widestrs] - pub fn set_from_string(&mut self, str: wcharz_t) { + pub fn set_from_string<'a>(&mut self, str: impl Into<&'a wstr>) { let str: &wstr = str.into(); let whitespace = "\t\n\0x0B\0x0C\r "L.as_char_slice(); for entry in str.as_char_slice().split(|c| *c == ',') { @@ -232,12 +232,10 @@ pub fn feature_metadata() -> [feature_metadata_t; metadata.len()] { #[test] #[widestrs] fn test_feature_flags() { - use crate::wchar_ffi::wcharz; - let mut f = features_t::new(); - f.set_from_string(wcharz!("stderr-nocaret,nonsense"L)); + f.set_from_string("stderr-nocaret,nonsense"L); assert!(f.test(feature_flag_t::stderr_nocaret)); - f.set_from_string(wcharz!("stderr-nocaret,no-stderr-nocaret,nonsense"L)); + f.set_from_string("stderr-nocaret,no-stderr-nocaret,nonsense"L); assert!(f.test(feature_flag_t::stderr_nocaret)); // Ensure every metadata is represented once. diff --git a/fish-rust/src/util.rs b/fish-rust/src/util.rs index 59b48991f..f9c651b06 100644 --- a/fish-rust/src/util.rs +++ b/fish-rust/src/util.rs @@ -30,11 +30,11 @@ fn ordering_to_int(ord: Ordering) -> i32 { } fn wcsfilecmp_glob_ffi(a: wcharz_t, b: wcharz_t) -> i32 { - ordering_to_int(wcsfilecmp_glob(a, b)) + ordering_to_int(wcsfilecmp_glob(a.into(), b.into())) } fn wcsfilecmp_ffi(a: wcharz_t, b: wcharz_t) -> i32 { - ordering_to_int(wcsfilecmp(a, b)) + ordering_to_int(wcsfilecmp(a.into(), b.into())) } /// Compares two wide character strings with an (arguably) intuitive ordering. This function tries @@ -64,10 +64,7 @@ fn wcsfilecmp_ffi(a: wcharz_t, b: wcharz_t) -> i32 { /// a 'file1' and 'File1' will not be considered identical, and hence their internal sort order is /// not arbitrary, but the names 'file1', 'File2' and 'file3' will still be sorted in the order /// given above. -pub fn wcsfilecmp(a: wcharz_t, b: wcharz_t) -> Ordering { - // TODO This should return `std::cmp::Ordering`. - let a: &wstr = a.into(); - let b: &wstr = b.into(); +pub fn wcsfilecmp(a: &wstr, b: &wstr) -> Ordering { let mut retval = Ordering::Equal; let mut ai = 0; let mut bi = 0; @@ -134,10 +131,7 @@ pub fn wcsfilecmp(a: wcharz_t, b: wcharz_t) -> Ordering { } /// wcsfilecmp, but frozen in time for glob usage. -pub fn wcsfilecmp_glob(a: wcharz_t, b: wcharz_t) -> Ordering { - // TODO This should return `std::cmp::Ordering`. - let a: &wstr = a.into(); - let b: &wstr = b.into(); +pub fn wcsfilecmp_glob(a: &wstr, b: &wstr) -> Ordering { let mut retval = Ordering::Equal; let mut ai = 0; let mut bi = 0; @@ -268,14 +262,10 @@ fn wcsfilecmp_leading_digits(a: &wstr, b: &wstr) -> (Ordering, usize, usize) { #[test] fn test_wcsfilecmp() { use crate::wchar::L; - use crate::wchar_ffi::wcharz; macro_rules! validate { ($str1:expr, $str2:expr, $expected_rc:expr) => { - assert_eq!( - wcsfilecmp(wcharz!(L!($str1)), wcharz!(L!($str2))), - $expected_rc - ) + assert_eq!(wcsfilecmp(L!($str1), L!($str2)), $expected_rc) }; }