@@ -70,10 +70,10 @@ use core::prelude::*;
70
70
use alloc:: libc_heap:: malloc_raw;
71
71
use collections:: string:: String ;
72
72
use collections:: hash;
73
+ use core:: fmt;
73
74
use core:: kinds:: marker;
74
75
use core:: mem;
75
76
use core:: ptr;
76
- use core:: fmt;
77
77
use core:: raw:: Slice ;
78
78
use core:: slice;
79
79
use core:: str;
@@ -93,23 +93,18 @@ impl Clone for CString {
93
93
/// reasons, this is always a deep clone, rather than the usual shallow
94
94
/// clone.
95
95
fn clone ( & self ) -> CString {
96
- if self . buf . is_null ( ) {
97
- CString { buf : self . buf , owns_buffer_ : self . owns_buffer_ }
98
- } else {
99
- let len = self . len ( ) + 1 ;
100
- let buf = unsafe { malloc_raw ( len) } as * mut libc:: c_char ;
101
- unsafe { ptr:: copy_nonoverlapping_memory ( buf, self . buf , len) ; }
102
- CString { buf : buf as * const libc:: c_char , owns_buffer_ : true }
103
- }
96
+ let len = self . len ( ) + 1 ;
97
+ let buf = unsafe { malloc_raw ( len) } as * mut libc:: c_char ;
98
+ unsafe { ptr:: copy_nonoverlapping_memory ( buf, self . buf , len) ; }
99
+ CString { buf : buf as * const libc:: c_char , owns_buffer_ : true }
104
100
}
105
101
}
106
102
107
103
impl PartialEq for CString {
108
104
fn eq ( & self , other : & CString ) -> bool {
105
+ // Check if the two strings share the same buffer
109
106
if self . buf as uint == other. buf as uint {
110
107
true
111
- } else if self . buf . is_null ( ) || other. buf . is_null ( ) {
112
- false
113
108
} else {
114
109
unsafe {
115
110
libc:: strcmp ( self . buf , other. buf ) == 0
@@ -136,7 +131,12 @@ impl<S: hash::Writer> hash::Hash<S> for CString {
136
131
137
132
impl CString {
138
133
/// Create a C String from a pointer.
134
+ ///
135
+ ///# Failure
136
+ ///
137
+ /// Fails if `buf` is null
139
138
pub unsafe fn new ( buf : * const libc:: c_char , owns_buffer : bool ) -> CString {
139
+ assert ! ( !buf. is_null( ) ) ;
140
140
CString { buf : buf, owns_buffer_ : owns_buffer }
141
141
}
142
142
@@ -158,10 +158,6 @@ impl CString {
158
158
/// let p = foo.to_c_str().as_ptr();
159
159
/// ```
160
160
///
161
- /// # Failure
162
- ///
163
- /// Fails if the CString is null.
164
- ///
165
161
/// # Example
166
162
///
167
163
/// ```rust
@@ -175,8 +171,6 @@ impl CString {
175
171
/// }
176
172
/// ```
177
173
pub fn as_ptr ( & self ) -> * const libc:: c_char {
178
- if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
179
-
180
174
self . buf
181
175
}
182
176
@@ -197,44 +191,30 @@ impl CString {
197
191
/// // wrong (the CString will be freed, invalidating `p`)
198
192
/// let p = foo.to_c_str().as_mut_ptr();
199
193
/// ```
200
- ///
201
- /// # Failure
202
- ///
203
- /// Fails if the CString is null.
204
194
pub fn as_mut_ptr ( & mut self ) -> * mut libc:: c_char {
205
- if self . buf . is_null ( ) { fail ! ( "CString is null!" ) }
206
-
207
195
self . buf as * mut _
208
196
}
209
197
210
198
/// Calls a closure with a reference to the underlying `*libc::c_char`.
211
- ///
212
- /// # Failure
213
- ///
214
- /// Fails if the CString is null.
215
199
#[ deprecated="use `.as_ptr()`" ]
216
200
pub fn with_ref < T > ( & self , f : |* const libc:: c_char | -> T ) -> T {
217
- if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
218
201
f ( self . buf )
219
202
}
220
203
221
204
/// Calls a closure with a mutable reference to the underlying `*libc::c_char`.
222
- ///
223
- /// # Failure
224
- ///
225
- /// Fails if the CString is null.
226
205
#[ deprecated="use `.as_mut_ptr()`" ]
227
206
pub fn with_mut_ref < T > ( & mut self , f : |* mut libc:: c_char | -> T ) -> T {
228
- if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
229
207
f ( self . buf as * mut libc:: c_char )
230
208
}
231
209
232
210
/// Returns true if the CString is a null.
211
+ #[ deprecated="a CString cannot be null" ]
233
212
pub fn is_null ( & self ) -> bool {
234
213
self . buf . is_null ( )
235
214
}
236
215
237
216
/// Returns true if the CString is not null.
217
+ #[ deprecated="a CString cannot be null" ]
238
218
pub fn is_not_null ( & self ) -> bool {
239
219
self . buf . is_not_null ( )
240
220
}
@@ -246,51 +226,32 @@ impl CString {
246
226
247
227
/// Converts the CString into a `&[u8]` without copying.
248
228
/// Includes the terminating NUL byte.
249
- ///
250
- /// # Failure
251
- ///
252
- /// Fails if the CString is null.
253
229
#[ inline]
254
230
pub fn as_bytes < ' a > ( & ' a self ) -> & ' a [ u8 ] {
255
- if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
256
231
unsafe {
257
232
mem:: transmute ( Slice { data : self . buf , len : self . len ( ) + 1 } )
258
233
}
259
234
}
260
235
261
236
/// Converts the CString into a `&[u8]` without copying.
262
237
/// Does not include the terminating NUL byte.
263
- ///
264
- /// # Failure
265
- ///
266
- /// Fails if the CString is null.
267
238
#[ inline]
268
239
pub fn as_bytes_no_nul < ' a > ( & ' a self ) -> & ' a [ u8 ] {
269
- if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
270
240
unsafe {
271
241
mem:: transmute ( Slice { data : self . buf , len : self . len ( ) } )
272
242
}
273
243
}
274
244
275
245
/// Converts the CString into a `&str` without copying.
276
246
/// Returns None if the CString is not UTF-8.
277
- ///
278
- /// # Failure
279
- ///
280
- /// Fails if the CString is null.
281
247
#[ inline]
282
248
pub fn as_str < ' a > ( & ' a self ) -> Option < & ' a str > {
283
249
let buf = self . as_bytes_no_nul ( ) ;
284
250
str:: from_utf8 ( buf)
285
251
}
286
252
287
253
/// Return a CString iterator.
288
- ///
289
- /// # Failure
290
- ///
291
- /// Fails if the CString is null.
292
254
pub fn iter < ' a > ( & ' a self ) -> CChars < ' a > {
293
- if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
294
255
CChars {
295
256
ptr : self . buf ,
296
257
marker : marker:: ContravariantLifetime ,
@@ -326,13 +287,8 @@ impl Drop for CString {
326
287
327
288
impl Collection for CString {
328
289
/// Return the number of bytes in the CString (not including the NUL terminator).
329
- ///
330
- /// # Failure
331
- ///
332
- /// Fails if the CString is null.
333
290
#[ inline]
334
291
fn len ( & self ) -> uint {
335
- if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
336
292
let mut cur = self . buf ;
337
293
let mut len = 0 ;
338
294
unsafe {
@@ -631,13 +587,6 @@ mod tests {
631
587
}
632
588
}
633
589
634
- #[ test]
635
- fn test_is_null ( ) {
636
- let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
637
- assert ! ( c_str. is_null( ) ) ;
638
- assert ! ( !c_str. is_not_null( ) ) ;
639
- }
640
-
641
590
#[ test]
642
591
fn test_unwrap ( ) {
643
592
let c_str = "hello" . to_c_str ( ) ;
@@ -648,16 +597,8 @@ mod tests {
648
597
fn test_as_ptr ( ) {
649
598
let c_str = "hello" . to_c_str ( ) ;
650
599
let len = unsafe { libc:: strlen ( c_str. as_ptr ( ) ) } ;
651
- assert ! ( !c_str. is_null( ) ) ;
652
- assert ! ( c_str. is_not_null( ) ) ;
653
600
assert_eq ! ( len, 5 ) ;
654
601
}
655
- #[ test]
656
- #[ should_fail]
657
- fn test_as_ptr_empty_fail ( ) {
658
- let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
659
- c_str. as_ptr ( ) ;
660
- }
661
602
662
603
#[ test]
663
604
fn test_iterator ( ) {
@@ -716,20 +657,6 @@ mod tests {
716
657
assert_eq ! ( c_str. as_bytes_no_nul( ) , b"foo\xFF " ) ;
717
658
}
718
659
719
- #[ test]
720
- #[ should_fail]
721
- fn test_as_bytes_fail ( ) {
722
- let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
723
- c_str. as_bytes ( ) ;
724
- }
725
-
726
- #[ test]
727
- #[ should_fail]
728
- fn test_as_bytes_no_nul_fail ( ) {
729
- let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
730
- c_str. as_bytes_no_nul ( ) ;
731
- }
732
-
733
660
#[ test]
734
661
fn test_as_str ( ) {
735
662
let c_str = "hello" . to_c_str ( ) ;
@@ -742,23 +669,8 @@ mod tests {
742
669
743
670
#[ test]
744
671
#[ should_fail]
745
- fn test_as_str_fail ( ) {
746
- let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
747
- c_str. as_str ( ) ;
748
- }
749
-
750
- #[ test]
751
- #[ should_fail]
752
- fn test_len_fail ( ) {
672
+ fn test_new_fail ( ) {
753
673
let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
754
- c_str. len ( ) ;
755
- }
756
-
757
- #[ test]
758
- #[ should_fail]
759
- fn test_iter_fail ( ) {
760
- let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
761
- c_str. iter ( ) ;
762
674
}
763
675
764
676
#[ test]
@@ -791,13 +703,6 @@ mod tests {
791
703
// force a copy, reading the memory
792
704
c_. as_bytes ( ) . to_vec ( ) ;
793
705
}
794
-
795
- #[ test]
796
- fn test_clone_eq_null ( ) {
797
- let x = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
798
- let y = x. clone ( ) ;
799
- assert ! ( x == y) ;
800
- }
801
706
}
802
707
803
708
#[ cfg( test) ]
0 commit comments