diff --git a/array.rs b/array.rs index 8117f65..105f8e7 100644 --- a/array.rs +++ b/array.rs @@ -47,9 +47,7 @@ pub impl CFArray { let array_ref : CFArrayRef; - let elems_refs = do vec::map(elems) |e| { - e.as_type_ref() - }; + let elems_refs = do vec::map(elems) |e: &ElemType| { e.get_ref().as_type_ref() }; unsafe { array_ref = CFArrayCreate(kCFAllocatorDefault, @@ -81,6 +79,9 @@ pub impl> CFArray : AbstractCFType { + + pure fn get_ref() -> CFArrayRef { self.obj } + static fn wrap(obj: CFArrayRef) -> CFArray { CFArray { obj: obj } } @@ -88,12 +89,6 @@ pub impl) -> CFArrayRef { wrapper.obj } - - pure fn as_type_ref(&self) -> CFTypeRef { - unsafe { - cast::transmute(self.obj) - } - } } pub impl { - pure fn as_type_ref(&self) -> CFTypeRef; + pure fn get_ref() -> T; static fn wrap(T) -> self; static fn unwrap(wrapper: self) -> T; } @@ -49,9 +49,8 @@ impl CFTypeRef : AbstractCFTypeRef { } impl CFType : AbstractCFType { - pure fn as_type_ref(&self) -> CFTypeRef { - self.obj - } + pure fn get_ref() -> CFTypeRef { self.obj } + static fn wrap(obj: CFTypeRef) -> CFType { CFType { obj: obj } } @@ -62,30 +61,44 @@ impl CFType : AbstractCFType { } trait CFTypeOps { - fn as_type(&self) -> CFType; - static fn wrap_borrowed(T) -> self; + pure fn get_type_ref(&self) -> CFTypeRef; + static fn as_CFType(o: self) -> CFType; + fn clone_as_CFType(&self) -> CFType; + static fn clone(&T) -> self; fn retain_count(&self) -> CFIndex; fn show(&self); } -impl> S : CFTypeOps { - // FIXME: Should move, but there's a linearity bug. - fn as_type(&self) -> CFType { - CFRetain(self.as_type_ref()); - CFType { obj: self.as_type_ref() } +impl> S : CFTypeOps { + // not actually unsafe + pure fn get_type_ref(&self) -> CFTypeRef unsafe { + self.get_ref().as_type_ref() } - static fn wrap_borrowed(cfref: T) -> S { + // FIXME: Should move, but there's a linearity bug. + static fn as_CFType(obj: S) -> CFType { + let tyref : CFTypeRef = base::unwrap(move obj).as_type_ref(); + CFType { obj: tyref } + } + + // FIXME: Should move, but there's a linearity bug. + fn clone_as_CFType(&self) -> CFType { + let tyref = self.get_ref().as_type_ref(); + CFRetain(tyref); + CFType { obj: tyref } + } + + static fn clone(cfref: &T) -> S { CFRetain(cfref.as_type_ref()); - base::wrap(move cfref) + base::wrap(copy *cfref) } fn retain_count(&self) -> CFIndex { - CFGetRetainCount(self.as_type_ref()) + CFGetRetainCount(self.get_type_ref()) } fn show(&self) { - CFShow(self.as_type_ref()); + CFShow(self.get_type_ref()); } } diff --git a/boolean.rs b/boolean.rs index ea59fde..e18348c 100644 --- a/boolean.rs +++ b/boolean.rs @@ -41,9 +41,7 @@ pub impl CFBoolean { } impl CFBoolean : AbstractCFType { - pure fn as_type_ref(&self) -> CFTypeRef { - unsafe { cast::transmute(self.obj) } - } + pure fn get_ref() -> CFBooleanRef { self.obj } static fn wrap(obj: CFBooleanRef) -> CFBoolean { CFBoolean { obj: obj } diff --git a/data.rs b/data.rs index d5ec227..f78b434 100644 --- a/data.rs +++ b/data.rs @@ -30,9 +30,8 @@ struct CFData { } impl CFData : AbstractCFType { - pure fn as_type_ref(&self) -> CFTypeRef { - self.obj.as_type_ref() - } + pure fn get_ref() -> CFDataRef { self.obj } + static fn wrap(obj: CFDataRef) -> CFData { CFData { obj: obj } } diff --git a/dictionary.rs b/dictionary.rs index c5582e8..742f324 100644 --- a/dictionary.rs +++ b/dictionary.rs @@ -53,11 +53,8 @@ pub impl, ValueType : AbstractCFType> CFDictionary : AbstractCFType { - pure fn as_type_ref(&self) -> CFTypeRef { - unsafe { self.obj.as_type_ref() } - } - pure fn borrow_ref(&self) -> &self/CFDictionaryRef { &self.obj } + pure fn get_ref() -> CFDictionaryRef { self.obj } static fn wrap(obj: CFDictionaryRef) -> CFDictionary { CFDictionary { obj: obj } @@ -80,8 +77,9 @@ pub impl { - keys.push(key.as_type_ref()); - values.push(value.as_type_ref()); + // TODO: should be able to say key.get_type_ref(), but resolve isn't having any of it. + keys.push(key.get_ref().as_type_ref()); + values.push(value.get_ref().as_type_ref()); } } } diff --git a/number.rs b/number.rs index 4bb1478..f975c6b 100644 --- a/number.rs +++ b/number.rs @@ -102,9 +102,7 @@ pub impl CFNumber { } impl CFNumber : AbstractCFType { - pure fn as_type_ref(&self) -> CFTypeRef { - unsafe { cast::transmute(self.obj) } - } + pure fn get_ref() -> CFNumberRef { self.obj } static fn wrap(obj: CFNumberRef) -> CFNumber { CFNumber { obj: obj } diff --git a/set.rs b/set.rs index 7aa225c..a00d061 100644 --- a/set.rs +++ b/set.rs @@ -42,11 +42,7 @@ pub struct CFSet { } pub impl CFSet : AbstractCFType { - pure fn as_type_ref(&self) -> CFTypeRef { - unsafe { - self.obj.as_type_ref() - } - } + pure fn get_ref() -> CFSetRef { self.obj } static fn wrap(obj: CFSetRef) -> CFSet { CFSet { obj: obj } diff --git a/string.rs b/string.rs index 3065816..23f4457 100644 --- a/string.rs +++ b/string.rs @@ -28,11 +28,7 @@ pub struct CFString { } pub impl CFString : AbstractCFType { - pure fn as_type_ref(&self) -> CFTypeRef { - unsafe { - reinterpret_cast(&self.obj) - } - } + pure fn get_ref() -> CFStringRef { self.obj } static fn wrap(obj: CFStringRef) -> CFString { CFString { obj: obj } diff --git a/test.rs b/test.rs index c0dac37..55598ba 100644 --- a/test.rs +++ b/test.rs @@ -1,17 +1,17 @@ use base::CFTypeOps; -use boolean::CFBoolean; -use number::CFNumber; +use boolean::{CFBoolean, CFBooleanRef}; +use number::{CFNumber, CFNumberRef}; use dictionary::CFDictionary; -use string::CFString; +use string::{CFString, CFStringRef}; fn main() { /*let n = CFNumber::new_number(42 as i32); io::println(fmt!("%d", (&n).retain_count() as int)); (&n).show();*/ let d = CFDictionary::new([ - (CFString::new_static("Foo"), (&CFString::new_static("Bar")).as_type()), - (CFString::new_static("Baz"), (&CFBoolean::true_value()).as_type()), - (CFString::new_static("Boo"), (&CFNumber::new(42 as i32)).as_type()), + (CFString::new_static("Foo"), base::as_CFType::(CFString::new_static("Bar"))), + (CFString::new_static("Baz"), base::as_CFType::(CFBoolean::true_value())), + (CFString::new_static("Boo"), base::as_CFType::(CFNumber::new(42 as i32))), ]); (&d).show(); } diff --git a/url.rs b/url.rs index 33decd5..1484bc4 100644 --- a/url.rs +++ b/url.rs @@ -32,9 +32,8 @@ struct CFURL { } impl CFURL : AbstractCFType { - pure fn as_type_ref(&self) -> CFTypeRef { - self.obj.as_type_ref() - } + pure fn get_ref() -> CFURLRef { self.obj } + static fn wrap(obj: CFURLRef) -> CFURL { CFURL { obj: obj } }