Skip to content

Commit f63101f

Browse files
committed
Make the constructors return Option
This is a breaking change. See a [PR](rust-lang/rust#42959).
1 parent 9d8c8bf commit f63101f

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

src/lib.rs

+24-38
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,21 @@ where
8383
/// the head element of the list or is NULL for a list of length 0.
8484
/// `next` is a function that takes an element and returns an immutable raw
8585
/// 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+
})
9193
}
9294

9395
/// Returns the length of the `CLinkedList`.
9496
pub fn len(&self) -> usize {
9597
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;
99101
ret += 1;
100102
}
101103
ret
@@ -119,11 +121,13 @@ where
119121
/// the head element of the list or is NULL for a list of length 0.
120122
/// `next` is a function that takes an element and returns a mutable raw
121123
/// 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+
})
127131
}
128132

129133
/// Provides a forward iterator with mutable references.
@@ -137,9 +141,9 @@ where
137141
/// Returns the length of the `CLinkedList`.
138142
pub fn len(&self) -> usize {
139143
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;
143147
ret += 1;
144148
}
145149
ret
@@ -359,19 +363,10 @@ mod tests {
359363
#[test]
360364
fn test_using_const_ptr() {
361365
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());
372367

373368
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();
375370
let vs = list.iter().map(|n| n.val).collect::<Vec<_>>();
376371
assert_eq!(vs, &[1, 2, 3]);
377372
assert_eq!(list.len(), 3);
@@ -413,19 +408,10 @@ mod tests {
413408
#[test]
414409
fn test_using_mut_ptr() {
415410
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());
426412

427413
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();
429415
let vs = list.iter().map(|n| n.val).collect::<Vec<_>>();
430416
assert_eq!(vs, &[1, 2, 3]);
431417
assert_eq!(list.len(), 3);

0 commit comments

Comments
 (0)