From 52b5150cfd3dfcdf518675e9073f03e061a63a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Mon, 2 Feb 2015 21:45:49 +0100 Subject: [PATCH] Avoid ptrtoint when checking if a pointer is null Casting the pointer to an integer requires a ptrtoint, while casting 0 to a pointer is directly folded to a `null` value. --- src/libcore/ptr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 072c60c7036c..2779e67c7430 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -303,7 +303,7 @@ impl PtrExt for *const T { #[inline] #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool { self as usize == 0 } + fn is_null(self) -> bool { self == 0 as *const T } #[inline] #[stable(feature = "rust1", since = "1.0.0")] @@ -330,7 +330,7 @@ impl PtrExt for *mut T { #[inline] #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool { self as usize == 0 } + fn is_null(self) -> bool { self == 0 as *mut T } #[inline] #[stable(feature = "rust1", since = "1.0.0")]