@@ -148,10 +148,10 @@ pub struct CStringIterator<'self> {
148
148
149
149
impl < ' self > Iterator < libc:: c_char > for CStringIterator < ' self > {
150
150
fn next ( & mut self ) -> Option < libc:: c_char > {
151
- if self . ptr . is_null ( ) {
151
+ let ch = unsafe { * self . ptr } ;
152
+ if ch == 0 {
152
153
None
153
154
} else {
154
- let ch = unsafe { * self . ptr } ;
155
155
self . ptr = ptr:: offset ( self . ptr , 1 ) ;
156
156
Some ( ch)
157
157
}
@@ -163,6 +163,7 @@ mod tests {
163
163
use super :: * ;
164
164
use libc;
165
165
use ptr;
166
+ use option:: { Some , None } ;
166
167
167
168
#[ test]
168
169
fn test_to_c_str ( ) {
@@ -210,7 +211,23 @@ mod tests {
210
211
#[ should_fail]
211
212
#[ ignore( cfg( windows) ) ]
212
213
fn test_with_ref_empty_fail ( ) {
213
- let c_str = CString :: new ( ptr:: null ( ) , false ) ;
214
+ let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
214
215
c_str. with_ref ( |_| ( ) ) ;
215
216
}
217
+
218
+ #[ test]
219
+ fn test_iterator ( ) {
220
+ let c_str = "" . to_c_str ( ) ;
221
+ let mut iter = c_str. iter ( ) ;
222
+ assert_eq ! ( iter. next( ) , None ) ;
223
+
224
+ let c_str = "hello" . to_c_str ( ) ;
225
+ let mut iter = c_str. iter ( ) ;
226
+ assert_eq ! ( iter. next( ) , Some ( 'h' as libc:: c_char) ) ;
227
+ assert_eq ! ( iter. next( ) , Some ( 'e' as libc:: c_char) ) ;
228
+ assert_eq ! ( iter. next( ) , Some ( 'l' as libc:: c_char) ) ;
229
+ assert_eq ! ( iter. next( ) , Some ( 'l' as libc:: c_char) ) ;
230
+ assert_eq ! ( iter. next( ) , Some ( 'o' as libc:: c_char) ) ;
231
+ assert_eq ! ( iter. next( ) , None ) ;
232
+ }
216
233
}
0 commit comments