@@ -83,19 +83,21 @@ where
83
83
/// the head element of the list or is NULL for a list of length 0.
84
84
/// `next` is a function that takes an element and returns an immutable raw
85
85
/// pointer to the next element.
86
- pub unsafe fn from_const_ptr ( head : * const T , next : F ) -> Self {
87
- Self {
88
- element : Shared :: new ( head as * mut T ) ,
89
- next : next,
90
- }
86
+ pub fn from_const_ptr ( head : * const T , next : F ) -> Option < Self > {
87
+ Shared :: new ( head as * mut T ) . map ( |p| {
88
+ Self {
89
+ element : p,
90
+ next : next,
91
+ }
92
+ } )
91
93
}
92
94
93
95
/// Returns the length of the `CLinkedList`.
94
96
pub fn len ( & self ) -> usize {
95
97
let mut e = self . element ;
96
- let mut ret = 0 ;
97
- while !e . as_ptr ( ) . is_null ( ) {
98
- e = unsafe { Shared :: new ( ( self . next ) ( e . as_ref ( ) ) as * mut T ) } ;
98
+ let mut ret = 1 ;
99
+ while let Some ( p ) = Shared :: new ( ( self . next ) ( unsafe { e . as_ref ( ) } ) as * mut T ) {
100
+ e = p ;
99
101
ret += 1 ;
100
102
}
101
103
ret
@@ -119,11 +121,13 @@ where
119
121
/// the head element of the list or is NULL for a list of length 0.
120
122
/// `next` is a function that takes an element and returns a mutable raw
121
123
/// pointer to the next element.
122
- pub unsafe fn from_mut_ptr ( head : * mut T , next : F ) -> Self {
123
- Self {
124
- element : Shared :: new ( head) ,
125
- next : next,
126
- }
124
+ pub fn from_mut_ptr ( head : * mut T , next : F ) -> Option < Self > {
125
+ Shared :: new ( head) . map ( |p| {
126
+ Self {
127
+ element : p,
128
+ next : next,
129
+ }
130
+ } )
127
131
}
128
132
129
133
/// Provides a forward iterator with mutable references.
@@ -137,9 +141,9 @@ where
137
141
/// Returns the length of the `CLinkedList`.
138
142
pub fn len ( & self ) -> usize {
139
143
let mut e = self . element ;
140
- let mut ret = 0 ;
141
- while !e . as_ptr ( ) . is_null ( ) {
142
- e = unsafe { Shared :: new ( ( self . next ) ( e . as_ref ( ) ) ) } ;
144
+ let mut ret = 1 ;
145
+ while let Some ( p ) = Shared :: new ( ( self . next ) ( unsafe { e . as_ref ( ) } ) ) {
146
+ e = p ;
143
147
ret += 1 ;
144
148
}
145
149
ret
@@ -359,19 +363,10 @@ mod tests {
359
363
#[ test]
360
364
fn test_using_const_ptr ( ) {
361
365
let ptr: * const TestNodeConst = std:: ptr:: null ( ) ;
362
- let list = unsafe { CLinkedList :: from_const_ptr ( ptr, |n| n. next ) } ;
363
- let vs = list. iter ( ) . map ( |n| n. val ) . collect :: < Vec < _ > > ( ) ;
364
- assert_eq ! ( vs, & [ ] ) ;
365
- assert_eq ! ( list. len( ) , 0 ) ;
366
- assert ! ( !list. contains( & TestNodeConst {
367
- val: 0 ,
368
- next: std:: ptr:: null( ) ,
369
- } ) ) ;
370
- assert ! ( list. is_empty( ) ) ;
371
- assert ! ( list. front( ) . is_none( ) ) ;
366
+ assert ! ( CLinkedList :: from_const_ptr( ptr, |n| n. next) . is_none( ) ) ;
372
367
373
368
let ptr = make_list_const ( ) ;
374
- let list = unsafe { CLinkedList :: from_const_ptr ( ptr, |n| n. next ) } ;
369
+ let list = CLinkedList :: from_const_ptr ( ptr, |n| n. next ) . unwrap ( ) ;
375
370
let vs = list. iter ( ) . map ( |n| n. val ) . collect :: < Vec < _ > > ( ) ;
376
371
assert_eq ! ( vs, & [ 1 , 2 , 3 ] ) ;
377
372
assert_eq ! ( list. len( ) , 3 ) ;
@@ -413,19 +408,10 @@ mod tests {
413
408
#[ test]
414
409
fn test_using_mut_ptr ( ) {
415
410
let ptr: * mut TestNodeMut = std:: ptr:: null_mut ( ) ;
416
- let list = unsafe { CLinkedList :: from_mut_ptr ( ptr, |n| n. next ) } ;
417
- let vs = list. iter ( ) . map ( |n| n. val ) . collect :: < Vec < _ > > ( ) ;
418
- assert_eq ! ( vs, & [ ] ) ;
419
- assert_eq ! ( list. len( ) , 0 ) ;
420
- assert ! ( !list. contains( & TestNodeMut {
421
- val: 0 ,
422
- next: std:: ptr:: null_mut( ) ,
423
- } ) ) ;
424
- assert ! ( list. is_empty( ) ) ;
425
- assert ! ( list. front( ) . is_none( ) ) ;
411
+ assert ! ( CLinkedList :: from_mut_ptr( ptr, |n| n. next) . is_none( ) ) ;
426
412
427
413
let ptr = make_list_mut ( ) ;
428
- let mut list = unsafe { CLinkedList :: from_mut_ptr ( ptr, |n| n. next ) } ;
414
+ let mut list = CLinkedList :: from_mut_ptr ( ptr, |n| n. next ) . unwrap ( ) ;
429
415
let vs = list. iter ( ) . map ( |n| n. val ) . collect :: < Vec < _ > > ( ) ;
430
416
assert_eq ! ( vs, & [ 1 , 2 , 3 ] ) ;
431
417
assert_eq ! ( list. len( ) , 3 ) ;
0 commit comments