Hi!

On 2024-08-01T16:56:44+0200, Arthur Cohen <arthur.co...@embecosm.com> wrote:
> --- a/libgrust/libformat_parser/src/lib.rs
> +++ b/libgrust/libformat_parser/src/lib.rs

> [...]
> +    let rust_string = RustString {
> +        len: str.len(),
> +        cap: str.capacity(),
> +        ptr: str.leak().as_ptr(),
> +    };
> [...]

> [...]
> +    let RustString { ptr, len, cap } = *s;
> +    let s = unsafe { String::from_raw_parts(ptr as *mut u8, len, cap) };
> +    let cloned_s = s.clone();
> +
> +    // FIXME: Documentation
> +    s.leak();
> +
> +    let rust_string = RustString {
> +        len: cloned_s.len(),
> +        cap: cloned_s.capacity(),
> +        ptr: cloned_s.leak().as_ptr(),
> +    };
> [...]

OK to push the attached
"Rust: Work around 'error[E0599]: no method named `leak` found for struct 
`std::string::String` in the current scope'"?
Builds and tests fine, but I don't know if this code path is actually
exercised at this time, so please check carefully; as you know I'm not
actually a Rust programmer (yet).  ;-)


Grüße
 Thomas


>From 8d4821dabcf4663e51c7db859801837710038821 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <tschwi...@baylibre.com>
Date: Sat, 3 Aug 2024 16:39:17 +0200
Subject: [PATCH] Rust: Work around 'error[E0599]: no method named `leak` found
 for struct `std::string::String` in the current scope'

Compiling with Debian GNU/Linux 12 (bookworm) packages:

    $ apt-cache madison cargo rustc
         cargo | 0.66.0+ds1-1 | http://deb.debian.org/debian bookworm/main ppc64el Packages
         cargo | 0.66.0+ds1-1 | http://deb.debian.org/debian bookworm/main Sources
         rustc | 1.63.0+dfsg1-2 | http://deb.debian.org/debian bookworm/main ppc64el Packages
         rustc | 1.63.0+dfsg1-2 | http://deb.debian.org/debian bookworm/main Sources

..., we run into:

       Compiling libformat_parser v0.1.0 ([...]/source-gcc/libgrust/libformat_parser)
    error[E0599]: no method named `leak` found for struct `std::string::String` in the current scope
       --> src/lib.rs:396:18
        |
    396 |         ptr: str.leak().as_ptr(),
        |                  ^^^^ method not found in `std::string::String`

    error[E0599]: no method named `leak` found for struct `std::string::String` in the current scope
       --> src/lib.rs:434:7
        |
    434 |     s.leak();
        |       ^^^^ method not found in `std::string::String`

    error[E0599]: no method named `leak` found for struct `std::string::String` in the current scope
       --> src/lib.rs:439:23
        |
    439 |         ptr: cloned_s.leak().as_ptr(),
        |                       ^^^^ method not found in `std::string::String`

Locally replace 1.72.0+ method 'leak' for struct 'std::string::String'.

	libgrust/
	* libformat_parser/src/lib.rs: Work around 'error[E0599]:
	no method named `leak` found for struct `std::string::String` in the current scope'.
---
 libgrust/libformat_parser/src/lib.rs | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/libgrust/libformat_parser/src/lib.rs b/libgrust/libformat_parser/src/lib.rs
index 84fac38e224d..28f6a6a62b62 100644
--- a/libgrust/libformat_parser/src/lib.rs
+++ b/libgrust/libformat_parser/src/lib.rs
@@ -5,6 +5,13 @@
 
 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 IntoFFI<T> {
     fn into_ffi(self) -> T;
 }
@@ -393,7 +400,7 @@ pub extern "C" fn collect_pieces(
     let rust_string = RustString {
         len: str.len(),
         cap: str.capacity(),
-        ptr: str.leak().as_ptr(),
+        ptr: leak_string(str).as_ptr(),
     };
 
     FormatArgsHandle(piece_slice, rust_string)
@@ -431,12 +438,12 @@ pub extern "C" fn clone_pieces(
     let cloned_s = s.clone();
 
     // FIXME: Documentation
-    s.leak();
+    leak_string(s);
 
     let rust_string = RustString {
         len: cloned_s.len(),
         cap: cloned_s.capacity(),
-        ptr: cloned_s.leak().as_ptr(),
+        ptr: leak_string(cloned_s).as_ptr(),
     };
 
     FormatArgsHandle(piece_slice, rust_string)
-- 
2.34.1

Reply via email to