Skip to content

Commit 912f113

Browse files
committed
---
yaml --- r: 60573 b: refs/heads/auto c: adaae45 h: refs/heads/master i: 60571: 08a76b4 v: v3
1 parent fc60cf4 commit 912f113

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1414
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1515
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1616
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
17-
refs/heads/auto: 6c0a4693c94ffbb5601b59d0c82c7785d347107d
17+
refs/heads/auto: adaae45c3e15f95b052648f3511a1097155296b9
1818
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1919
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c

branches/auto/src/libcore/ptr.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use cast;
1414
use libc;
1515
use libc::{c_void, size_t};
16+
use option::{Option, Some, None};
1617
use sys;
1718

1819
#[cfg(not(test))] use cmp::{Eq, Ord};
@@ -209,6 +210,7 @@ pub unsafe fn array_each<T>(arr: **T, cb: &fn(*T)) {
209210
pub trait Ptr<T> {
210211
fn is_null(&const self) -> bool;
211212
fn is_not_null(&const self) -> bool;
213+
unsafe fn to_option(&const self) -> Option<&T>;
212214
fn offset(&self, count: uint) -> Self;
213215
}
214216

@@ -222,6 +224,23 @@ impl<T> Ptr<T> for *T {
222224
#[inline(always)]
223225
fn is_not_null(&const self) -> bool { is_not_null(*self) }
224226

227+
///
228+
/// Returns `None` if the pointer is null, or else returns the value wrapped
229+
/// in `Some`.
230+
///
231+
/// # Safety Notes
232+
///
233+
/// While this method is useful for null-safety, it is important to note
234+
/// that this is still an unsafe operation because the returned value could
235+
/// be pointing to invalid memory.
236+
///
237+
#[inline(always)]
238+
unsafe fn to_option(&const self) -> Option<&T> {
239+
if self.is_null() { None } else {
240+
Some(cast::transmute(*self))
241+
}
242+
}
243+
225244
/// Calculates the offset from a pointer.
226245
#[inline(always)]
227246
fn offset(&self, count: uint) -> *T { offset(*self, count) }
@@ -237,6 +256,23 @@ impl<T> Ptr<T> for *mut T {
237256
#[inline(always)]
238257
fn is_not_null(&const self) -> bool { is_not_null(*self) }
239258

259+
///
260+
/// Returns `None` if the pointer is null, or else returns the value wrapped
261+
/// in `Some`.
262+
///
263+
/// # Safety Notes
264+
///
265+
/// While this method is useful for null-safety, it is important to note
266+
/// that this is still an unsafe operation because the returned value could
267+
/// be pointing to invalid memory.
268+
///
269+
#[inline(always)]
270+
unsafe fn to_option(&const self) -> Option<&T> {
271+
if self.is_null() { None } else {
272+
Some(cast::transmute(*self))
273+
}
274+
}
275+
240276
/// Calculates the offset from a mutable pointer.
241277
#[inline(always)]
242278
fn offset(&self, count: uint) -> *mut T { mut_offset(*self, count) }
@@ -423,6 +459,21 @@ pub mod ptr_tests {
423459
assert!(mq.is_not_null());
424460
}
425461

462+
#[test]
463+
fn test_to_option() {
464+
let p: *int = null();
465+
// FIXME (#6641): Usage of unsafe methods in safe code doesn't cause an error.
466+
assert_eq!(p.to_option(), None);
467+
468+
let q: *int = &2;
469+
assert_eq!(q.to_option().unwrap(), &2); // FIXME (#6641)
470+
471+
let p: *mut int = mut_null();
472+
assert_eq!(p.to_option(), None); // FIXME (#6641)
473+
474+
let q: *mut int = &mut 2;
475+
assert_eq!(q.to_option().unwrap(), &2); // FIXME (#6641)
476+
}
426477

427478
#[test]
428479
fn test_ptr_array_each_with_len() {

0 commit comments

Comments
 (0)