]> git.proxmox.com Git - rustc.git/blob - vendor/core-foundation-sys/src/base.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / vendor / core-foundation-sys / src / base.rs
1 // Copyright 2013-2015 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 use std::cmp::Ordering;
11 use std::os::raw::{c_uint, c_void, c_int};
12 use string::CFStringRef;
13
14 pub type Boolean = u8;
15 pub type mach_port_t = c_uint;
16 pub type CFAllocatorRef = *const c_void;
17 pub type CFNullRef = *const c_void;
18 pub type CFTypeRef = *const c_void;
19 pub type OSStatus = i32;
20 pub type SInt32 = c_int;
21 pub type CFTypeID = usize;
22 pub type CFOptionFlags = usize;
23 pub type CFHashCode = usize;
24 pub type CFIndex = isize;
25
26 #[repr(isize)]
27 #[derive(Clone, Copy, Debug, PartialEq)]
28 pub enum CFComparisonResult {
29 LessThan = -1,
30 EqualTo = 0,
31 GreaterThan = 1,
32 }
33
34 impl Into<Ordering> for CFComparisonResult {
35 fn into(self) -> Ordering {
36 match self {
37 CFComparisonResult::LessThan => Ordering::Less,
38 CFComparisonResult::EqualTo => Ordering::Equal,
39 CFComparisonResult::GreaterThan => Ordering::Greater
40 }
41 }
42 }
43
44 #[repr(C)]
45 #[derive(Clone, Copy)]
46 pub struct CFRange {
47 pub location: CFIndex,
48 pub length: CFIndex
49 }
50
51 // for back-compat
52 impl CFRange {
53 pub fn init(location: CFIndex, length: CFIndex) -> CFRange {
54 CFRange {
55 location: location,
56 length: length,
57 }
58 }
59 }
60
61 pub type CFAllocatorRetainCallBack = extern "C" fn(info: *mut c_void) -> *mut c_void;
62 pub type CFAllocatorReleaseCallBack = extern "C" fn(info: *mut c_void);
63 pub type CFAllocatorCopyDescriptionCallBack = extern "C" fn(info: *mut c_void) -> CFStringRef;
64 pub type CFAllocatorAllocateCallBack = extern "C" fn(allocSize: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> *mut c_void;
65 pub type CFAllocatorReallocateCallBack = extern "C" fn(ptr: *mut c_void, newsize: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> *mut c_void;
66 pub type CFAllocatorDeallocateCallBack = extern "C" fn(ptr: *mut c_void, info: *mut c_void);
67 pub type CFAllocatorPreferredSizeCallBack = extern "C" fn(size: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> CFIndex;
68
69 #[repr(C)]
70 #[derive(Clone, Copy)]
71 pub struct CFAllocatorContext {
72 pub version: CFIndex,
73 pub info: *mut c_void,
74 pub retain: Option<CFAllocatorRetainCallBack>,
75 pub release: Option<CFAllocatorReleaseCallBack>,
76 pub copyDescription: Option<CFAllocatorCopyDescriptionCallBack>,
77 pub allocate: Option<CFAllocatorAllocateCallBack>,
78 pub reallocate: Option<CFAllocatorReallocateCallBack>,
79 pub deallocate: Option<CFAllocatorDeallocateCallBack>,
80 pub preferredSize: Option<CFAllocatorPreferredSizeCallBack>
81 }
82
83 /// Trait for all types which are Core Foundation reference types.
84 pub trait TCFTypeRef {
85 fn as_void_ptr(&self) -> *const c_void;
86
87 unsafe fn from_void_ptr(ptr: *const c_void) -> Self;
88 }
89
90 impl<T> TCFTypeRef for *const T {
91 fn as_void_ptr(&self) -> *const c_void {
92 (*self) as *const c_void
93 }
94
95 unsafe fn from_void_ptr(ptr: *const c_void) -> Self {
96 ptr as *const T
97 }
98 }
99
100 impl<T> TCFTypeRef for *mut T {
101 fn as_void_ptr(&self) -> *const c_void {
102 (*self) as *const T as *const c_void
103 }
104
105 unsafe fn from_void_ptr(ptr: *const c_void) -> Self {
106 ptr as *const T as *mut T
107 }
108 }
109
110 /// Constant used by some functions to indicate failed searches.
111 pub static kCFNotFound: CFIndex = -1;
112
113 extern {
114 /*
115 * CFBase.h
116 */
117
118 /* CFAllocator Reference */
119
120 pub static kCFAllocatorDefault: CFAllocatorRef;
121 pub static kCFAllocatorSystemDefault: CFAllocatorRef;
122 pub static kCFAllocatorMalloc: CFAllocatorRef;
123 pub static kCFAllocatorMallocZone: CFAllocatorRef;
124 pub static kCFAllocatorNull: CFAllocatorRef;
125 pub static kCFAllocatorUseContext: CFAllocatorRef;
126
127 pub fn CFAllocatorCreate(allocator: CFAllocatorRef, context: *mut CFAllocatorContext) -> CFAllocatorRef;
128 pub fn CFAllocatorAllocate(allocator: CFAllocatorRef, size: CFIndex, hint: CFOptionFlags) -> *mut c_void;
129 pub fn CFAllocatorDeallocate(allocator: CFAllocatorRef, ptr: *mut c_void);
130 pub fn CFAllocatorGetPreferredSizeForSize(allocator: CFAllocatorRef, size: CFIndex, hint: CFOptionFlags) -> CFIndex;
131 pub fn CFAllocatorReallocate(allocator: CFAllocatorRef, ptr: *mut c_void, newsize: CFIndex, hint: CFOptionFlags) -> *mut c_void;
132 pub fn CFAllocatorGetDefault() -> CFAllocatorRef;
133 pub fn CFAllocatorSetDefault(allocator: CFAllocatorRef);
134 pub fn CFAllocatorGetContext(allocator: CFAllocatorRef, context: *mut CFAllocatorContext);
135 pub fn CFAllocatorGetTypeID() -> CFTypeID;
136
137 /* CFNull Reference */
138
139 pub static kCFNull: CFNullRef;
140
141 /* CFType Reference */
142
143 //fn CFCopyTypeIDDescription
144 //fn CFGetAllocator
145 pub fn CFCopyDescription(cf: CFTypeRef) -> CFStringRef;
146 pub fn CFEqual(cf1: CFTypeRef, cf2: CFTypeRef) -> Boolean;
147 pub fn CFGetRetainCount(cf: CFTypeRef) -> CFIndex;
148 pub fn CFGetTypeID(cf: CFTypeRef) -> CFTypeID;
149 pub fn CFHash(cf: CFTypeRef) -> CFHashCode;
150 //fn CFMakeCollectable
151 pub fn CFRelease(cf: CFTypeRef);
152 pub fn CFRetain(cf: CFTypeRef) -> CFTypeRef;
153 pub fn CFShow(obj: CFTypeRef);
154
155 /* Base Utilities Reference */
156 // N.B. Some things missing here.
157 }