Skip to content
This repository was archived by the owner on Nov 26, 2020. It is now read-only.

Commit 229ebee

Browse files
committed
Lifetime anchors for raw string conversion functions
Instead of obtaining the lifetime from a raw pointer, provide it explicitly in the manner of rust-lang/rfcs#556.
1 parent 37e4549 commit 229ebee

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl ErrorTrait for Error {
111111
let mut os: Option<&'a str> = None;
112112
let raw = unsafe { &*self.ptr };
113113
if !raw.message.is_null() {
114-
os = unsafe { gstr::parse_as_utf8(&raw.message).ok() };
114+
os = unsafe { gstr::parse_as_utf8(raw.message, self).ok() };
115115
}
116116
if os.is_none() {
117117
let domain = unsafe { Quark::new(raw.domain) };
@@ -128,9 +128,9 @@ impl ErrorTrait for Error {
128128

129129
fn detail(&self) -> Option<String> {
130130
if !self.ptr.is_null() {
131-
let msg = unsafe { &(*self.ptr).message };
131+
let msg = unsafe { (*self.ptr).message };
132132
if !msg.is_null() {
133-
let msg_bytes = unsafe { gstr::parse_as_bytes(msg) };
133+
let msg_bytes = unsafe { gstr::parse_as_bytes(msg, self) };
134134
return Some(String::from_utf8_lossy(msg_bytes).into_owned());
135135
}
136136
}

src/gstr.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,21 @@ pub struct GStr {
3434
ptr: *const gchar,
3535
}
3636

37-
pub unsafe fn parse_as_bytes(raw: & *const gchar) -> &[u8] {
37+
pub unsafe fn parse_as_bytes<'a, T: ?Sized>(raw: *const gchar,
38+
life_anchor: &'a T)
39+
-> &'a [u8]
40+
{
3841
assert!(!raw.is_null());
39-
let r = mem::copy_lifetime(raw, &(*raw as *const u8));
40-
slice::from_raw_buf(r, libc::strlen(*raw) as uint)
42+
let r = mem::copy_lifetime(life_anchor, &(raw as *const u8));
43+
slice::from_raw_buf(r, libc::strlen(raw) as uint)
4144
}
4245

4346
#[inline]
44-
pub unsafe fn parse_as_utf8(raw: & *const gchar)
45-
-> Result<&str, str::Utf8Error>
47+
pub unsafe fn parse_as_utf8<'a, T: ?Sized>(raw: *const gchar,
48+
life_anchor: &'a T)
49+
-> Result<&'a str, str::Utf8Error>
4650
{
47-
str::from_utf8(parse_as_bytes(raw))
48-
}
49-
50-
#[inline]
51-
pub unsafe fn parse_as_utf8_unchecked(raw: & *const gchar) -> &str {
52-
str::from_utf8_unchecked(parse_as_bytes(raw))
51+
str::from_utf8(parse_as_bytes(raw, life_anchor))
5352
}
5453

5554
impl GStr {

src/gtype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl GType {
7474
impl fmt::Show for GType {
7575
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7676
let name = unsafe { ffi::g_type_name(self.to_raw()) };
77-
match unsafe { gstr::parse_as_utf8(&name) } {
77+
match unsafe { gstr::parse_as_utf8(name, "") } {
7878
Ok(s) => write!(f, "{}", s),
7979
Err(..) => Err(fmt::Error)
8080
}

src/quark.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use ffi;
2020
use gstr;
2121
use types::gchar;
2222

23-
use std::mem;
2423
use std::str;
2524
use std::sync::atomic;
2625

@@ -67,8 +66,7 @@ impl Quark {
6766
let Quark(raw) = *self;
6867
unsafe {
6968
let s = ffi::g_quark_to_string(raw);
70-
let r = mem::copy_lifetime("", &s);
71-
gstr::parse_as_bytes(r)
69+
gstr::parse_as_bytes(s, "")
7270
}
7371
}
7472

src/value.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ impl Value {
163163
if s.is_null() {
164164
return None;
165165
}
166-
let r = mem::copy_lifetime(self, &s);
167-
Some(gstr::parse_as_bytes(r))
166+
Some(gstr::parse_as_bytes(s, self))
168167
}
169168
}
170169

0 commit comments

Comments
 (0)