1 // Copyright 2013 The Servo Project Developers. See the COPYRIGHT 2 // file at the top-level directory of this distribution. 3 // 4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 7 // option. This file may not be copied, modified, or distributed 8 // except according to those terms. 9 10 //! Immutable numbers. 11 12 use core_foundation_sys::base::kCFAllocatorDefault; 13 pub use core_foundation_sys::number::*; 14 use std::os::raw::c_void; 15 16 use base::TCFType; 17 18 19 declare_TCFType!{ 20 /// An immutable numeric value. 21 CFNumber, CFNumberRef 22 } 23 impl_TCFType!(CFNumber, CFNumberRef, CFNumberGetTypeID); 24 impl_CFTypeDescription!(CFNumber); 25 impl_CFComparison!(CFNumber, CFNumberCompare); 26 27 impl CFNumber { 28 29 #[inline] to_i32(&self) -> Option<i32>30 pub fn to_i32(&self) -> Option<i32> { 31 unsafe { 32 let mut value: i32 = 0; 33 let ok = CFNumberGetValue(self.0, kCFNumberSInt32Type, &mut value as *mut i32 as *mut c_void); 34 if ok { Some(value) } else { None } 35 } 36 } 37 38 #[inline] to_i64(&self) -> Option<i64>39 pub fn to_i64(&self) -> Option<i64> { 40 unsafe { 41 let mut value: i64 = 0; 42 let ok = CFNumberGetValue(self.0, kCFNumberSInt64Type, &mut value as *mut i64 as *mut c_void); 43 if ok { Some(value) } else { None } 44 } 45 } 46 47 #[inline] to_f32(&self) -> Option<f32>48 pub fn to_f32(&self) -> Option<f32> { 49 unsafe { 50 let mut value: f32 = 0.0; 51 let ok = CFNumberGetValue(self.0, kCFNumberFloat32Type, &mut value as *mut f32 as *mut c_void); 52 if ok { Some(value) } else { None } 53 } 54 } 55 56 #[inline] to_f64(&self) -> Option<f64>57 pub fn to_f64(&self) -> Option<f64> { 58 unsafe { 59 let mut value: f64 = 0.0; 60 let ok = CFNumberGetValue(self.0, kCFNumberFloat64Type, &mut value as *mut f64 as *mut c_void); 61 if ok { Some(value) } else { None } 62 } 63 } 64 } 65 66 impl From<i32> for CFNumber { 67 #[inline] from(value: i32) -> Self68 fn from(value: i32) -> Self { 69 unsafe { 70 let number_ref = CFNumberCreate( 71 kCFAllocatorDefault, 72 kCFNumberSInt32Type, 73 &value as *const i32 as *const c_void, 74 ); 75 TCFType::wrap_under_create_rule(number_ref) 76 } 77 } 78 } 79 80 impl From<i64> for CFNumber { 81 #[inline] from(value: i64) -> Self82 fn from(value: i64) -> Self { 83 unsafe { 84 let number_ref = CFNumberCreate( 85 kCFAllocatorDefault, 86 kCFNumberSInt64Type, 87 &value as *const i64 as *const c_void, 88 ); 89 TCFType::wrap_under_create_rule(number_ref) 90 } 91 } 92 } 93 94 impl From<f32> for CFNumber { 95 #[inline] from(value: f32) -> Self96 fn from(value: f32) -> Self { 97 unsafe { 98 let number_ref = CFNumberCreate( 99 kCFAllocatorDefault, 100 kCFNumberFloat32Type, 101 &value as *const f32 as *const c_void, 102 ); 103 TCFType::wrap_under_create_rule(number_ref) 104 } 105 } 106 } 107 108 impl From<f64> for CFNumber { 109 #[inline] from(value: f64) -> Self110 fn from(value: f64) -> Self { 111 unsafe { 112 let number_ref = CFNumberCreate( 113 kCFAllocatorDefault, 114 kCFNumberFloat64Type, 115 &value as *const f64 as *const c_void, 116 ); 117 TCFType::wrap_under_create_rule(number_ref) 118 } 119 } 120 } 121