https://gcc.gnu.org/g:be38c37fdc35e7f77dbab16736782cfe24769e77

commit r15-6027-gbe38c37fdc35e7f77dbab16736782cfe24769e77
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Tue Apr 23 14:13:21 2024 +0200

    Rust: libformat_parser: Lower minimum Rust version to 1.49
    
    libgrust/ChangeLog:
    
            * libformat_parser/Cargo.toml: Change Rust edition from 2021 to 
2018.
            * libformat_parser/generic_format_parser/Cargo.toml: Likewise.
            * libformat_parser/generic_format_parser/src/lib.rs: Remove usage of
            then-unstable std features and language constructs.
            * libformat_parser/src/lib.rs: Likewise, plus provide extension 
trait
            for String::leak.

Diff:
---
 libgrust/libformat_parser/Cargo.toml               |  2 +-
 .../generic_format_parser/Cargo.toml               |  2 +-
 .../generic_format_parser/src/lib.rs               | 42 +++++++++++++++-------
 libgrust/libformat_parser/src/lib.rs               | 19 +++++-----
 4 files changed, 42 insertions(+), 23 deletions(-)

diff --git a/libgrust/libformat_parser/Cargo.toml 
b/libgrust/libformat_parser/Cargo.toml
index 0fcfa3e89a4c..3c214915d31e 100644
--- a/libgrust/libformat_parser/Cargo.toml
+++ b/libgrust/libformat_parser/Cargo.toml
@@ -1,7 +1,7 @@
 [package]
 name = "libformat_parser"
 version = "0.1.0"
-edition = "2021"
+edition = "2018"
 
 [workspace]
 
diff --git a/libgrust/libformat_parser/generic_format_parser/Cargo.toml 
b/libgrust/libformat_parser/generic_format_parser/Cargo.toml
index 34577038cbed..224ad6826ec8 100644
--- a/libgrust/libformat_parser/generic_format_parser/Cargo.toml
+++ b/libgrust/libformat_parser/generic_format_parser/Cargo.toml
@@ -1,7 +1,7 @@
 [package]
 name = "generic_format_parser"
 version = "0.1.0"
-edition = "2021"
+edition = "2018"
 
 # See more keys and their definitions at 
https://doc.rust-lang.org/cargo/reference/manifest.html
 
diff --git a/libgrust/libformat_parser/generic_format_parser/src/lib.rs 
b/libgrust/libformat_parser/generic_format_parser/src/lib.rs
index ed3d8577befa..e255bf16908f 100644
--- a/libgrust/libformat_parser/generic_format_parser/src/lib.rs
+++ b/libgrust/libformat_parser/generic_format_parser/src/lib.rs
@@ -505,7 +505,7 @@ impl<'a> Parser<'a> {
             }
 
             pos = peek_pos;
-            description = format!("expected `'}}'`, found `{maybe:?}`");
+            description = format!("expected `'}}'`, found `{:?}`", maybe);
         } else {
             description = "expected `'}'` but string was 
terminated".to_owned();
             // point at closing `"`
@@ -690,12 +690,16 @@ impl<'a> Parser<'a> {
 
         // fill character
         if let Some(&(idx, c)) = self.cur.peek() {
-            if let Some((_, '>' | '<' | '^')) = self.cur.clone().nth(1) {
-                spec.fill = Some(c);
-                spec.fill_span = Some(self.span(idx, idx + 1));
-                self.cur.next();
+            match self.cur.clone().nth(1) {
+                Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => {
+                    spec.fill = Some(c);
+                    spec.fill_span = Some(self.span(idx, idx + 1));
+                    self.cur.next();
+                }
+                _ => {}
             }
         }
+
         // Alignment
         if self.consume('<') {
             spec.align = AlignLeft;
@@ -908,7 +912,11 @@ impl<'a> Parser<'a> {
             );
         }
 
-        found.then_some(cur)
+        if found {
+            Some(cur)
+        } else {
+            None
+        }
     }
 
     fn suggest_format(&mut self) {
@@ -992,10 +1000,8 @@ fn find_width_map_from_snippet(
     // Alternatively, we could just count the trailing newlines and only trim 
one from the input if they don't match up.
     let input_no_nl = input.trim_end_matches('\n');
     let unescaped = match unescape_string(snippet) {
-        Some(unescaped) => unescaped,
-        _ => {
-            return InputStringKind::NotALiteral;
-        }
+        Some(u) => u,
+        None => return InputStringKind::NotALiteral,
     };
 
     let unescaped_no_nl = unescaped.trim_end_matches('\n');
@@ -1026,7 +1032,13 @@ fn find_width_map_from_snippet(
 
                 width_mappings.push(InnerWidthMapping::new(pos, width, 0));
             }
-            ('\\', Some((_, 'n' | 't' | 'r' | '0' | '\\' | '\'' | '\"'))) => {
+            ('\\', Some((_, 'n')))
+            | ('\\', Some((_, 't')))
+            | ('\\', Some((_, 'r')))
+            | ('\\', Some((_, '0')))
+            | ('\\', Some((_, '\\')))
+            | ('\\', Some((_, '\'')))
+            | ('\\', Some((_, '\"'))) => {
                 width_mappings.push(InnerWidthMapping::new(pos, 2, 1));
                 let _ = s.next();
             }
@@ -1052,7 +1064,7 @@ fn find_width_map_from_snippet(
                             .as_str()
                             .get(..digits_len)
                             .and_then(|digits| u32::from_str_radix(digits, 
16).ok())
-                            .and_then(char::from_u32)
+                            .and_then(std::char::from_u32)
                             .map_or(1, char::len_utf8);
 
                         // Skip the digits, for chars that encode to more than 
1 utf-8 byte
@@ -1108,7 +1120,11 @@ fn unescape_string(string: &str) -> 
Option<string::String> {
     let buf = string::String::from(string);
     let ok = true;
 
-    ok.then_some(buf)
+    if ok {
+        Some(buf)
+    } else {
+        None
+    }
 }
 
 // Assert a reasonable size for `Piece`
diff --git a/libgrust/libformat_parser/src/lib.rs 
b/libgrust/libformat_parser/src/lib.rs
index 28f6a6a62b62..0769577740f4 100644
--- a/libgrust/libformat_parser/src/lib.rs
+++ b/libgrust/libformat_parser/src/lib.rs
@@ -5,11 +5,14 @@
 
 use std::ffi::CStr;
 
-// Local replacement for 1.72.0+ method 'leak' for struct 
'std::string::String',
-// <https://doc.rust-lang.org/1.72.0/src/alloc/string.rs.html#1853>
-fn leak_string<'a>(s: String) -> &'a mut str {
-    let slice = s.into_bytes().leak();
-    unsafe { std::str::from_utf8_unchecked_mut(slice) }
+trait StringLeakExt {
+    fn leak<'a>(self) -> &'a mut str;
+}
+
+impl StringLeakExt for String {
+    fn leak<'a>(self) -> &'a mut str {
+        Box::leak(self.into_boxed_str())
+    }
 }
 
 trait IntoFFI<T> {
@@ -400,7 +403,7 @@ pub extern "C" fn collect_pieces(
     let rust_string = RustString {
         len: str.len(),
         cap: str.capacity(),
-        ptr: leak_string(str).as_ptr(),
+        ptr: str.leak().as_ptr(),
     };
 
     FormatArgsHandle(piece_slice, rust_string)
@@ -438,12 +441,12 @@ pub extern "C" fn clone_pieces(
     let cloned_s = s.clone();
 
     // FIXME: Documentation
-    leak_string(s);
+    s.leak();
 
     let rust_string = RustString {
         len: cloned_s.len(),
         cap: cloned_s.capacity(),
-        ptr: leak_string(cloned_s).as_ptr(),
+        ptr: cloned_s.leak().as_ptr(),
     };
 
     FormatArgsHandle(piece_slice, rust_string)

Reply via email to