This repository was archived by the owner on Nov 26, 2020. It is now read-only.
File tree 5 files changed +16
-20
lines changed
5 files changed +16
-20
lines changed Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ impl ErrorTrait for Error {
111
111
let mut os: Option < & ' a str > = None ;
112
112
let raw = unsafe { & * self . ptr } ;
113
113
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 ( ) } ;
115
115
}
116
116
if os. is_none ( ) {
117
117
let domain = unsafe { Quark :: new ( raw. domain ) } ;
@@ -128,9 +128,9 @@ impl ErrorTrait for Error {
128
128
129
129
fn detail ( & self ) -> Option < String > {
130
130
if !self . ptr . is_null ( ) {
131
- let msg = unsafe { & ( * self . ptr ) . message } ;
131
+ let msg = unsafe { ( * self . ptr ) . message } ;
132
132
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 ) } ;
134
134
return Some ( String :: from_utf8_lossy ( msg_bytes) . into_owned ( ) ) ;
135
135
}
136
136
}
Original file line number Diff line number Diff line change @@ -34,22 +34,21 @@ pub struct GStr {
34
34
ptr : * const gchar ,
35
35
}
36
36
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
+ {
38
41
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 )
41
44
}
42
45
43
46
#[ 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 >
46
50
{
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) )
53
52
}
54
53
55
54
impl GStr {
Original file line number Diff line number Diff line change @@ -74,7 +74,7 @@ impl GType {
74
74
impl fmt:: Show for GType {
75
75
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
76
76
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, "" ) } {
78
78
Ok ( s) => write ! ( f, "{}" , s) ,
79
79
Err ( ..) => Err ( fmt:: Error )
80
80
}
Original file line number Diff line number Diff line change @@ -20,7 +20,6 @@ use ffi;
20
20
use gstr;
21
21
use types:: gchar;
22
22
23
- use std:: mem;
24
23
use std:: str;
25
24
use std:: sync:: atomic;
26
25
@@ -67,8 +66,7 @@ impl Quark {
67
66
let Quark ( raw) = * self ;
68
67
unsafe {
69
68
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, "" )
72
70
}
73
71
}
74
72
Original file line number Diff line number Diff line change @@ -163,8 +163,7 @@ impl Value {
163
163
if s. is_null ( ) {
164
164
return None ;
165
165
}
166
- let r = mem:: copy_lifetime ( self , & s) ;
167
- Some ( gstr:: parse_as_bytes ( r) )
166
+ Some ( gstr:: parse_as_bytes ( s, self ) )
168
167
}
169
168
}
170
169
You can’t perform that action at this time.
0 commit comments