core_foundation/
number.rs

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
12use core_foundation_sys::base::kCFAllocatorDefault;
13pub use core_foundation_sys::number::*;
14use std::os::raw::c_void;
15
16use crate::base::TCFType;
17
18declare_TCFType! {
19    /// An immutable numeric value.
20    CFNumber, CFNumberRef
21}
22impl_TCFType!(CFNumber, CFNumberRef, CFNumberGetTypeID);
23impl_CFTypeDescription!(CFNumber);
24impl_CFComparison!(CFNumber, CFNumberCompare);
25
26impl CFNumber {
27    #[inline]
28    pub fn to_i32(&self) -> Option<i32> {
29        unsafe {
30            let mut value: i32 = 0;
31            let ok = CFNumberGetValue(
32                self.0,
33                kCFNumberSInt32Type,
34                &mut value as *mut i32 as *mut c_void,
35            );
36            if ok {
37                Some(value)
38            } else {
39                None
40            }
41        }
42    }
43
44    #[inline]
45    pub fn to_i64(&self) -> Option<i64> {
46        unsafe {
47            let mut value: i64 = 0;
48            let ok = CFNumberGetValue(
49                self.0,
50                kCFNumberSInt64Type,
51                &mut value as *mut i64 as *mut c_void,
52            );
53            if ok {
54                Some(value)
55            } else {
56                None
57            }
58        }
59    }
60
61    #[inline]
62    pub fn to_f32(&self) -> Option<f32> {
63        unsafe {
64            let mut value: f32 = 0.0;
65            let ok = CFNumberGetValue(
66                self.0,
67                kCFNumberFloat32Type,
68                &mut value as *mut f32 as *mut c_void,
69            );
70            if ok {
71                Some(value)
72            } else {
73                None
74            }
75        }
76    }
77
78    #[inline]
79    pub fn to_f64(&self) -> Option<f64> {
80        unsafe {
81            let mut value: f64 = 0.0;
82            let ok = CFNumberGetValue(
83                self.0,
84                kCFNumberFloat64Type,
85                &mut value as *mut f64 as *mut c_void,
86            );
87            if ok {
88                Some(value)
89            } else {
90                None
91            }
92        }
93    }
94}
95
96impl From<i32> for CFNumber {
97    #[inline]
98    fn from(value: i32) -> Self {
99        unsafe {
100            let number_ref = CFNumberCreate(
101                kCFAllocatorDefault,
102                kCFNumberSInt32Type,
103                &value as *const i32 as *const c_void,
104            );
105            TCFType::wrap_under_create_rule(number_ref)
106        }
107    }
108}
109
110impl From<i64> for CFNumber {
111    #[inline]
112    fn from(value: i64) -> Self {
113        unsafe {
114            let number_ref = CFNumberCreate(
115                kCFAllocatorDefault,
116                kCFNumberSInt64Type,
117                &value as *const i64 as *const c_void,
118            );
119            TCFType::wrap_under_create_rule(number_ref)
120        }
121    }
122}
123
124impl From<f32> for CFNumber {
125    #[inline]
126    fn from(value: f32) -> Self {
127        unsafe {
128            let number_ref = CFNumberCreate(
129                kCFAllocatorDefault,
130                kCFNumberFloat32Type,
131                &value as *const f32 as *const c_void,
132            );
133            TCFType::wrap_under_create_rule(number_ref)
134        }
135    }
136}
137
138impl From<f64> for CFNumber {
139    #[inline]
140    fn from(value: f64) -> Self {
141        unsafe {
142            let number_ref = CFNumberCreate(
143                kCFAllocatorDefault,
144                kCFNumberFloat64Type,
145                &value as *const f64 as *const c_void,
146            );
147            TCFType::wrap_under_create_rule(number_ref)
148        }
149    }
150}