@@ -10,6 +10,7 @@ use crate::{
10
10
} ;
11
11
use std:: ffi:: { CStr , CString } ;
12
12
use std:: fmt;
13
+ use std:: os:: raw:: c_char;
13
14
14
15
/// The value of a variable, register or expression.
15
16
pub struct SBValue {
@@ -53,33 +54,18 @@ impl SBValue {
53
54
}
54
55
55
56
#[ allow( missing_docs) ]
56
- pub fn name ( & self ) -> & str {
57
- unsafe {
58
- match CStr :: from_ptr ( sys:: SBValueGetName ( self . raw ) ) . to_str ( ) {
59
- Ok ( s) => s,
60
- _ => panic ! ( "Invalid string?" ) ,
61
- }
62
- }
57
+ pub fn name ( & self ) -> Option < & str > {
58
+ unsafe { self . check_null_ptr ( sys:: SBValueGetName ( self . raw ) ) }
63
59
}
64
60
65
61
#[ allow( missing_docs) ]
66
- pub fn type_name ( & self ) -> & str {
67
- unsafe {
68
- match CStr :: from_ptr ( sys:: SBValueGetTypeName ( self . raw ) ) . to_str ( ) {
69
- Ok ( s) => s,
70
- _ => panic ! ( "Invalid string?" ) ,
71
- }
72
- }
62
+ pub fn type_name ( & self ) -> Option < & str > {
63
+ unsafe { self . check_null_ptr ( sys:: SBValueGetTypeName ( self . raw ) ) }
73
64
}
74
65
75
66
#[ allow( missing_docs) ]
76
- pub fn display_type_name ( & self ) -> & str {
77
- unsafe {
78
- match CStr :: from_ptr ( sys:: SBValueGetDisplayTypeName ( self . raw ) ) . to_str ( ) {
79
- Ok ( s) => s,
80
- _ => panic ! ( "Invalid string?" ) ,
81
- }
82
- }
67
+ pub fn display_type_name ( & self ) -> Option < & str > {
68
+ unsafe { self . check_null_ptr ( sys:: SBValueGetDisplayTypeName ( self . raw ) ) }
83
69
}
84
70
85
71
#[ allow( missing_docs) ]
@@ -103,13 +89,8 @@ impl SBValue {
103
89
}
104
90
105
91
#[ allow( missing_docs) ]
106
- pub fn value ( & self ) -> & str {
107
- unsafe {
108
- match CStr :: from_ptr ( sys:: SBValueGetValue ( self . raw ) ) . to_str ( ) {
109
- Ok ( s) => s,
110
- _ => panic ! ( "Invalid string?" ) ,
111
- }
112
- }
92
+ pub fn value ( & self ) -> Option < & str > {
93
+ unsafe { self . check_null_ptr ( sys:: SBValueGetValue ( self . raw ) ) }
113
94
}
114
95
115
96
#[ allow( missing_docs) ]
@@ -257,6 +238,17 @@ impl SBValue {
257
238
pub fn address ( & self ) -> Option < SBAddress > {
258
239
SBAddress :: maybe_wrap ( unsafe { sys:: SBValueGetAddress ( self . raw ) } )
259
240
}
241
+
242
+ unsafe fn check_null_ptr ( & self , ptr : * const c_char ) -> Option < & str > {
243
+ if !ptr. is_null ( ) {
244
+ match CStr :: from_ptr ( ptr) . to_str ( ) {
245
+ Ok ( s) => Some ( s) ,
246
+ _ => panic ! ( "Invalid string?" ) ,
247
+ }
248
+ } else {
249
+ None
250
+ }
251
+ }
260
252
}
261
253
262
254
impl Clone for SBValue {
0 commit comments