Skip to content

Commit 48b1165

Browse files
committed
Use newtype structs.
1 parent c577bd6 commit 48b1165

File tree

10 files changed

+72
-116
lines changed

10 files changed

+72
-116
lines changed

src/base.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ impl CFIndexConvertible for usize {
3636

3737
pub type CFOptionFlags = u32;
3838

39-
#[allow(dead_code)]
4039
#[repr(C)]
4140
#[derive(Copy)]
4241
pub struct CFRange {
@@ -73,23 +72,21 @@ struct __CFType;
7372
pub type CFTypeRef = *const __CFType;
7473

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

8077
impl Clone for CFType {
8178
#[inline]
8279
fn clone(&self) -> CFType {
8380
unsafe {
84-
TCFType::wrap_under_get_rule(self.obj)
81+
TCFType::wrap_under_get_rule(self.as_concrete_TypeRef())
8582
}
8683
}
8784
}
8885

8986
impl Drop for CFType {
9087
fn drop(&mut self) {
9188
unsafe {
92-
CFRelease(self.obj)
89+
CFRelease(self.as_CFTypeRef())
9390
}
9491
}
9592
}
@@ -157,7 +154,8 @@ pub trait TCFType<ConcreteTypeRef> {
157154
impl TCFType<CFTypeRef> for CFType {
158155
#[inline]
159156
fn as_concrete_TypeRef(&self) -> CFTypeRef {
160-
self.obj
157+
let CFType(obj) = *self;
158+
obj
161159
}
162160

163161
#[inline]
@@ -173,9 +171,7 @@ impl TCFType<CFTypeRef> for CFType {
173171

174172
#[inline]
175173
unsafe fn wrap_under_create_rule(obj: CFTypeRef) -> CFType {
176-
CFType {
177-
obj: obj,
178-
}
174+
CFType(obj)
179175
}
180176

181177
#[inline]

src/boolean.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ struct __CFBoolean;
2020
pub type CFBooleanRef = *const __CFBoolean;
2121

2222
/// A Boolean type.
23-
///
24-
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
25-
pub struct CFBoolean {
26-
obj: CFBooleanRef,
27-
}
23+
pub struct CFBoolean(CFBooleanRef);
2824

2925
impl Drop for CFBoolean {
3026
fn drop(&mut self) {
@@ -37,7 +33,8 @@ impl Drop for CFBoolean {
3733
impl TCFType<CFBooleanRef> for CFBoolean {
3834
#[inline]
3935
fn as_concrete_TypeRef(&self) -> CFBooleanRef {
40-
self.obj
36+
let CFBoolean(obj) = *self;
37+
obj
4138
}
4239

4340
#[inline]
@@ -54,9 +51,7 @@ impl TCFType<CFBooleanRef> for CFBoolean {
5451
}
5552

5653
unsafe fn wrap_under_create_rule(obj: CFBooleanRef) -> CFBoolean {
57-
CFBoolean {
58-
obj: obj,
59-
}
54+
CFBoolean(obj)
6055
}
6156

6257
#[inline]

src/bundle.rs

Lines changed: 4 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,8 @@ impl Drop for CFBundle {
3834
impl TCFType<CFBundleRef> for CFBundle {
3935
#[inline]
4036
fn as_concrete_TypeRef(&self) -> CFBundleRef {
41-
self.obj
37+
let CFBundle(obj) = *self;
38+
obj
4239
}
4340

4441
#[inline]
@@ -55,9 +52,7 @@ impl TCFType<CFBundleRef> for CFBundle {
5552
}
5653

5754
unsafe fn wrap_under_create_rule(obj: CFBundleRef) -> CFBundle {
58-
CFBundle {
59-
obj: obj,
60-
}
55+
CFBundle(obj)
6156
}
6257

6358
#[inline]

src/data.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ struct __CFData;
2020
pub type CFDataRef = *const __CFData;
2121

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

2925
impl Drop for CFData {
3026
fn drop(&mut self) {
@@ -37,7 +33,8 @@ impl Drop for CFData {
3733
impl TCFType<CFDataRef> for CFData {
3834
#[inline]
3935
fn as_concrete_TypeRef(&self) -> CFDataRef {
40-
self.obj
36+
let CFData(obj) = *self;
37+
obj
4138
}
4239

4340
#[inline]
@@ -54,9 +51,7 @@ impl TCFType<CFDataRef> for CFData {
5451
}
5552

5653
unsafe fn wrap_under_create_rule(obj: CFDataRef) -> CFData {
57-
CFData {
58-
obj: obj,
59-
}
54+
CFData(obj)
6055
}
6156

6257
#[inline]
@@ -82,15 +77,17 @@ impl CFData {
8277
#[inline]
8378
pub fn bytes<'a>(&'a self) -> &'a [u8] {
8479
unsafe {
85-
mem::transmute((CFDataGetBytePtr(self.obj), self.len() as usize))
80+
mem::transmute(
81+
(CFDataGetBytePtr(self.as_concrete_TypeRef()), self.len() as usize)
82+
)
8683
}
8784
}
8885

8986
/// Returns the length of this byte buffer.
9087
#[inline]
9188
pub fn len(&self) -> CFIndex {
9289
unsafe {
93-
CFDataGetLength(self.obj)
90+
CFDataGetLength(self.as_concrete_TypeRef())
9491
}
9592
}
9693
}

src/dictionary.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub type CFDictionaryHashCallBack = *const u8;
2323
pub type CFDictionaryReleaseCallBack = *const u8;
2424
pub type CFDictionaryRetainCallBack = *const u8;
2525

26-
#[allow(dead_code)]
2726
#[repr(C)]
2827
#[derive(Copy)]
2928
pub struct CFDictionaryKeyCallBacks {
@@ -35,7 +34,6 @@ pub struct CFDictionaryKeyCallBacks {
3534
hash: CFDictionaryHashCallBack
3635
}
3736

38-
#[allow(dead_code)]
3937
#[repr(C)]
4038
#[derive(Copy)]
4139
pub struct CFDictionaryValueCallBacks {
@@ -52,11 +50,7 @@ struct __CFDictionary;
5250
pub type CFDictionaryRef = *const __CFDictionary;
5351

5452
/// 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-
}
53+
pub struct CFDictionary(CFDictionaryRef);
6054

6155
impl Drop for CFDictionary {
6256
fn drop(&mut self) {
@@ -69,7 +63,8 @@ impl Drop for CFDictionary {
6963
impl TCFType<CFDictionaryRef> for CFDictionary {
7064
#[inline]
7165
fn as_concrete_TypeRef(&self) -> CFDictionaryRef {
72-
self.obj
66+
let CFDictionary(obj) = *self;
67+
obj
7368
}
7469

7570
#[inline]
@@ -86,9 +81,7 @@ impl TCFType<CFDictionaryRef> for CFDictionary {
8681
}
8782

8883
unsafe fn wrap_under_create_rule(obj: CFDictionaryRef) -> CFDictionary {
89-
CFDictionary {
90-
obj: obj,
91-
}
84+
CFDictionary(obj)
9285
}
9386

9487
#[inline]
@@ -120,7 +113,7 @@ impl CFDictionary {
120113
#[inline]
121114
pub fn len(&self) -> usize {
122115
unsafe {
123-
CFDictionaryGetCount(self.obj) as usize
116+
CFDictionaryGetCount(self.as_concrete_TypeRef()) as usize
124117
}
125118
}
126119

@@ -132,15 +125,15 @@ impl CFDictionary {
132125
#[inline]
133126
pub fn contains_key(&self, key: *const c_void) -> bool {
134127
unsafe {
135-
CFDictionaryContainsKey(self.obj, key) != 0
128+
CFDictionaryContainsKey(self.as_concrete_TypeRef(), key) != 0
136129
}
137130
}
138131

139132
#[inline]
140133
pub fn find(&self, key: *const c_void) -> Option<*const c_void> {
141134
unsafe {
142135
let mut value: *const c_void = ptr::null();
143-
if CFDictionaryGetValueIfPresent(self.obj, key, &mut value) != 0 {
136+
if CFDictionaryGetValueIfPresent(self.as_concrete_TypeRef(), key, &mut value) != 0 {
144137
Some(value)
145138
} else {
146139
None

src/number.rs

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

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

5450
impl Drop for CFNumber {
5551
fn drop(&mut self) {
@@ -62,7 +58,8 @@ impl Drop for CFNumber {
6258
impl TCFType<CFNumberRef> for CFNumber {
6359
#[inline]
6460
fn as_concrete_TypeRef(&self) -> CFNumberRef {
65-
self.obj
61+
let CFNumber(obj) = *self;
62+
obj
6663
}
6764

6865
#[inline]
@@ -79,9 +76,7 @@ impl TCFType<CFNumberRef> for CFNumber {
7976
}
8077

8178
unsafe fn wrap_under_create_rule(obj: CFNumberRef) -> CFNumber {
82-
CFNumber {
83-
obj: obj,
84-
}
79+
CFNumber(obj)
8580
}
8681

8782
#[inline]
@@ -98,7 +93,8 @@ impl ToPrimitive for CFNumber {
9893
fn to_i64(&self) -> Option<i64> {
9994
unsafe {
10095
let mut value: i64 = 0;
101-
let ok = CFNumberGetValue(self.obj, kCFNumberSInt64Type, mem::transmute(&mut value));
96+
let obj = self.as_concrete_TypeRef();
97+
let ok = CFNumberGetValue(obj, kCFNumberSInt64Type, mem::transmute(&mut value));
10298
assert!(ok);
10399
Some(value)
104100
}
@@ -114,7 +110,8 @@ impl ToPrimitive for CFNumber {
114110
fn to_f64(&self) -> Option<f64> {
115111
unsafe {
116112
let mut value: f64 = 0.0;
117-
let ok = CFNumberGetValue(self.obj, kCFNumberFloat64Type, mem::transmute(&mut value));
113+
let obj = self.as_concrete_TypeRef();
114+
let ok = CFNumberGetValue(obj, kCFNumberFloat64Type, mem::transmute(&mut value));
118115
assert!(ok);
119116
Some(value)
120117
}

0 commit comments

Comments
 (0)