Skip to content

Commit d816f39

Browse files
committed
Use newtype structs
1 parent 6d5a95c commit d816f39

File tree

11 files changed

+58
-122
lines changed

11 files changed

+58
-122
lines changed

src/array.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ struct __CFArray;
4444
pub type CFArrayRef = *const __CFArray;
4545

4646
/// A heterogeneous immutable array.
47-
///
48-
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
49-
pub struct CFArray {
50-
obj: CFArrayRef,
51-
}
47+
pub struct CFArray(CFArrayRef);
5248

5349
impl Drop for CFArray {
5450
fn drop(&mut self) {
@@ -80,7 +76,7 @@ impl<'a> Iterator for CFArrayIterator<'a> {
8076
impl TCFType<CFArrayRef> for CFArray {
8177
#[inline]
8278
fn as_concrete_TypeRef(&self) -> CFArrayRef {
83-
self.obj
79+
self.0
8480
}
8581

8682
#[inline]
@@ -98,9 +94,7 @@ impl TCFType<CFArrayRef> for CFArray {
9894

9995
#[inline]
10096
unsafe fn wrap_under_create_rule(obj: CFArrayRef) -> CFArray {
101-
CFArray {
102-
obj: obj,
103-
}
97+
CFArray(obj)
10498
}
10599

106100
#[inline]
@@ -140,15 +134,15 @@ impl CFArray {
140134
#[inline]
141135
pub fn len(&self) -> CFIndex {
142136
unsafe {
143-
CFArrayGetCount(self.obj)
137+
CFArrayGetCount(self.0)
144138
}
145139
}
146140

147141
#[inline]
148142
pub fn get(&self, index: CFIndex) -> *const c_void {
149143
assert!(index < self.len());
150144
unsafe {
151-
CFArrayGetValueAtIndex(self.obj, index)
145+
CFArrayGetValueAtIndex(self.0, index)
152146
}
153147
}
154148
}

src/base.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,21 @@ struct __CFType;
7272
pub type CFTypeRef = *const __CFType;
7373

7474
/// Superclass of all Core Foundation objects.
75-
pub struct CFType {
76-
obj: CFTypeRef,
77-
}
75+
pub struct CFType(CFTypeRef);
7876

7977
impl Clone for CFType {
8078
#[inline]
8179
fn clone(&self) -> CFType {
8280
unsafe {
83-
TCFType::wrap_under_get_rule(self.obj)
81+
TCFType::wrap_under_get_rule(self.0)
8482
}
8583
}
8684
}
8785

8886
impl Drop for CFType {
8987
fn drop(&mut self) {
9088
unsafe {
91-
CFRelease(self.obj)
89+
CFRelease(self.0)
9290
}
9391
}
9492
}
@@ -156,7 +154,7 @@ pub trait TCFType<ConcreteTypeRef> {
156154
impl TCFType<CFTypeRef> for CFType {
157155
#[inline]
158156
fn as_concrete_TypeRef(&self) -> CFTypeRef {
159-
self.obj
157+
self.0
160158
}
161159

162160
#[inline]
@@ -172,9 +170,7 @@ impl TCFType<CFTypeRef> for CFType {
172170

173171
#[inline]
174172
unsafe fn wrap_under_create_rule(obj: CFTypeRef) -> CFType {
175-
CFType {
176-
obj: obj,
177-
}
173+
CFType(obj)
178174
}
179175

180176
#[inline]

src/boolean.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ pub type CFBooleanRef = *const __CFBoolean;
2222
/// A Boolean type.
2323
///
2424
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
25-
pub struct CFBoolean {
26-
obj: CFBooleanRef,
27-
}
25+
pub struct CFBoolean(CFBooleanRef);
2826

2927
impl Drop for CFBoolean {
3028
fn drop(&mut self) {
@@ -37,7 +35,7 @@ impl Drop for CFBoolean {
3735
impl TCFType<CFBooleanRef> for CFBoolean {
3836
#[inline]
3937
fn as_concrete_TypeRef(&self) -> CFBooleanRef {
40-
self.obj
38+
self.0
4139
}
4240

4341
#[inline]
@@ -54,9 +52,7 @@ impl TCFType<CFBooleanRef> for CFBoolean {
5452
}
5553

5654
unsafe fn wrap_under_create_rule(obj: CFBooleanRef) -> CFBoolean {
57-
CFBoolean {
58-
obj: obj,
59-
}
55+
CFBoolean(obj)
6056
}
6157

6258
#[inline]

src/bundle.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ struct __CFBundle;
2121
pub type CFBundleRef = *const __CFBundle;
2222

2323
/// A Bundle type.
24-
///
25-
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
26-
pub struct CFBundle {
27-
obj: CFBundleRef,
28-
}
24+
pub struct CFBundle(CFBundleRef);
2925

3026
impl Drop for CFBundle {
3127
fn drop(&mut self) {
@@ -38,7 +34,7 @@ impl Drop for CFBundle {
3834
impl TCFType<CFBundleRef> for CFBundle {
3935
#[inline]
4036
fn as_concrete_TypeRef(&self) -> CFBundleRef {
41-
self.obj
37+
self.0
4238
}
4339

4440
#[inline]
@@ -55,9 +51,7 @@ impl TCFType<CFBundleRef> for CFBundle {
5551
}
5652

5753
unsafe fn wrap_under_create_rule(obj: CFBundleRef) -> CFBundle {
58-
CFBundle {
59-
obj: obj,
60-
}
54+
CFBundle(obj)
6155
}
6256

6357
#[inline]

src/data.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ struct __CFData;
2222
pub type CFDataRef = *const __CFData;
2323

2424
/// A byte buffer.
25-
///
26-
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
27-
pub struct CFData {
28-
obj: CFDataRef,
29-
}
25+
pub struct CFData(CFDataRef);
3026

3127
impl Drop for CFData {
3228
fn drop(&mut self) {
@@ -39,7 +35,7 @@ impl Drop for CFData {
3935
impl TCFType<CFDataRef> for CFData {
4036
#[inline]
4137
fn as_concrete_TypeRef(&self) -> CFDataRef {
42-
self.obj
38+
self.0
4339
}
4440

4541
#[inline]
@@ -56,9 +52,7 @@ impl TCFType<CFDataRef> for CFData {
5652
}
5753

5854
unsafe fn wrap_under_create_rule(obj: CFDataRef) -> CFData {
59-
CFData {
60-
obj: obj,
61-
}
55+
CFData(obj)
6256
}
6357

6458
#[inline]
@@ -84,15 +78,15 @@ impl CFData {
8478
#[inline]
8579
pub fn bytes<'a>(&'a self) -> &'a [u8] {
8680
unsafe {
87-
slice::from_raw_parts(CFDataGetBytePtr(self.obj), self.len() as usize)
81+
slice::from_raw_parts(CFDataGetBytePtr(self.0), self.len() as usize)
8882
}
8983
}
9084

9185
/// Returns the length of this byte buffer.
9286
#[inline]
9387
pub fn len(&self) -> CFIndex {
9488
unsafe {
95-
CFDataGetLength(self.obj)
89+
CFDataGetLength(self.0)
9690
}
9791
}
9892
}

src/dictionary.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ struct __CFDictionary;
5252
pub type CFDictionaryRef = *const __CFDictionary;
5353

5454
/// An immutable dictionary of key-value pairs.
55-
///
56-
/// FIXME(pcwalton): Should be a newtype struct, but that panics due to a Rust compiler bug.
57-
pub struct CFDictionary {
58-
obj: CFDictionaryRef,
59-
}
55+
pub struct CFDictionary(CFDictionaryRef);
6056

6157
impl Drop for CFDictionary {
6258
fn drop(&mut self) {
@@ -69,7 +65,7 @@ impl Drop for CFDictionary {
6965
impl TCFType<CFDictionaryRef> for CFDictionary {
7066
#[inline]
7167
fn as_concrete_TypeRef(&self) -> CFDictionaryRef {
72-
self.obj
68+
self.0
7369
}
7470

7571
#[inline]
@@ -86,9 +82,7 @@ impl TCFType<CFDictionaryRef> for CFDictionary {
8682
}
8783

8884
unsafe fn wrap_under_create_rule(obj: CFDictionaryRef) -> CFDictionary {
89-
CFDictionary {
90-
obj: obj,
91-
}
85+
CFDictionary(obj)
9286
}
9387

9488
#[inline]
@@ -121,7 +115,7 @@ impl CFDictionary {
121115
#[inline]
122116
pub fn len(&self) -> usize {
123117
unsafe {
124-
CFDictionaryGetCount(self.obj) as usize
118+
CFDictionaryGetCount(self.0) as usize
125119
}
126120
}
127121

@@ -133,15 +127,15 @@ impl CFDictionary {
133127
#[inline]
134128
pub fn contains_key(&self, key: *const c_void) -> bool {
135129
unsafe {
136-
CFDictionaryContainsKey(self.obj, key) != 0
130+
CFDictionaryContainsKey(self.0, key) != 0
137131
}
138132
}
139133

140134
#[inline]
141135
pub fn find(&self, key: *const c_void) -> Option<*const c_void> {
142136
unsafe {
143137
let mut value: *const c_void = ptr::null();
144-
if CFDictionaryGetValueIfPresent(self.obj, key, &mut value) != 0 {
138+
if CFDictionaryGetValueIfPresent(self.0, key, &mut value) != 0 {
145139
Some(value)
146140
} else {
147141
None

src/number.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ struct __CFNumber;
4444
pub type CFNumberRef = *const __CFNumber;
4545

4646
/// An immutable numeric value.
47-
///
48-
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
49-
pub struct CFNumber {
50-
obj: CFNumberRef,
51-
}
47+
pub struct CFNumber(CFNumberRef);
5248

5349
impl Drop for CFNumber {
5450
fn drop(&mut self) {
@@ -61,7 +57,7 @@ impl Drop for CFNumber {
6157
impl TCFType<CFNumberRef> for CFNumber {
6258
#[inline]
6359
fn as_concrete_TypeRef(&self) -> CFNumberRef {
64-
self.obj
60+
self.0
6561
}
6662

6763
#[inline]
@@ -78,9 +74,7 @@ impl TCFType<CFNumberRef> for CFNumber {
7874
}
7975

8076
unsafe fn wrap_under_create_rule(obj: CFNumberRef) -> CFNumber {
81-
CFNumber {
82-
obj: obj,
83-
}
77+
CFNumber(obj)
8478
}
8579

8680
#[inline]
@@ -107,7 +101,7 @@ impl CFNumber {
107101
pub fn to_i64(&self) -> Option<i64> {
108102
unsafe {
109103
let mut value: i64 = 0;
110-
let ok = CFNumberGetValue(self.obj, kCFNumberSInt64Type, mem::transmute(&mut value));
104+
let ok = CFNumberGetValue(self.0, kCFNumberSInt64Type, mem::transmute(&mut value));
111105
if ok { Some(value) } else { None }
112106
}
113107
}
@@ -116,7 +110,7 @@ impl CFNumber {
116110
pub fn to_f64(&self) -> Option<f64> {
117111
unsafe {
118112
let mut value: f64 = 0.0;
119-
let ok = CFNumberGetValue(self.obj, kCFNumberFloat64Type, mem::transmute(&mut value));
113+
let ok = CFNumberGetValue(self.0, kCFNumberFloat64Type, mem::transmute(&mut value));
120114
if ok { Some(value) } else { None }
121115
}
122116
}

0 commit comments

Comments
 (0)