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

Commit

Permalink
Lifetime anchors for raw string conversion functions
Browse files Browse the repository at this point in the history
Instead of obtaining the lifetime from a raw pointer,
provide it explicitly in the manner of rust-lang/rfcs#556.
  • Loading branch information
mzabaluev committed Jan 8, 2015
1 parent 37e4549 commit 229ebee
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl ErrorTrait for Error {
let mut os: Option<&'a str> = None;
let raw = unsafe { &*self.ptr };
if !raw.message.is_null() {
os = unsafe { gstr::parse_as_utf8(&raw.message).ok() };
os = unsafe { gstr::parse_as_utf8(raw.message, self).ok() };
}
if os.is_none() {
let domain = unsafe { Quark::new(raw.domain) };
Expand All @@ -128,9 +128,9 @@ impl ErrorTrait for Error {

fn detail(&self) -> Option<String> {
if !self.ptr.is_null() {
let msg = unsafe { &(*self.ptr).message };
let msg = unsafe { (*self.ptr).message };
if !msg.is_null() {
let msg_bytes = unsafe { gstr::parse_as_bytes(msg) };
let msg_bytes = unsafe { gstr::parse_as_bytes(msg, self) };
return Some(String::from_utf8_lossy(msg_bytes).into_owned());
}
}
Expand Down
21 changes: 10 additions & 11 deletions src/gstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,21 @@ pub struct GStr {
ptr: *const gchar,
}

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

#[inline]
pub unsafe fn parse_as_utf8(raw: & *const gchar)
-> Result<&str, str::Utf8Error>
pub unsafe fn parse_as_utf8<'a, T: ?Sized>(raw: *const gchar,
life_anchor: &'a T)
-> Result<&'a str, str::Utf8Error>
{
str::from_utf8(parse_as_bytes(raw))
}

#[inline]
pub unsafe fn parse_as_utf8_unchecked(raw: & *const gchar) -> &str {
str::from_utf8_unchecked(parse_as_bytes(raw))
str::from_utf8(parse_as_bytes(raw, life_anchor))
}

impl GStr {
Expand Down
2 changes: 1 addition & 1 deletion src/gtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl GType {
impl fmt::Show for GType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = unsafe { ffi::g_type_name(self.to_raw()) };
match unsafe { gstr::parse_as_utf8(&name) } {
match unsafe { gstr::parse_as_utf8(name, "") } {
Ok(s) => write!(f, "{}", s),
Err(..) => Err(fmt::Error)
}
Expand Down
4 changes: 1 addition & 3 deletions src/quark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use ffi;
use gstr;
use types::gchar;

use std::mem;
use std::str;
use std::sync::atomic;

Expand Down Expand Up @@ -67,8 +66,7 @@ impl Quark {
let Quark(raw) = *self;
unsafe {
let s = ffi::g_quark_to_string(raw);
let r = mem::copy_lifetime("", &s);
gstr::parse_as_bytes(r)
gstr::parse_as_bytes(s, "")
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ impl Value {
if s.is_null() {
return None;
}
let r = mem::copy_lifetime(self, &s);
Some(gstr::parse_as_bytes(r))
Some(gstr::parse_as_bytes(s, self))
}
}

Expand Down

0 comments on commit 229ebee

Please sign in to comment.