Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CFPropertyList and methods for casting to subclasses #131

Merged
merged 4 commits into from Nov 24, 2017
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

Change downcast to return Option<T>

  • Loading branch information
faern committed Nov 24, 2017
commit cc1197c371c5dd6ef8f84912e31c225edb8128f5
@@ -11,7 +11,6 @@

use std::ptr;
use std::mem;
use std::fmt;

use libc::c_void;

@@ -196,33 +195,16 @@ impl CFPropertyList {
///
/// [`CFPropertyList`]: struct.CFPropertyList.html
/// [`Box::downcast`]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast
pub fn downcast<Raw, T: CFPropertyListSubClass<Raw>>(&self) -> Result<T, CFPropertyListCastError> {
pub fn downcast<Raw, T: CFPropertyListSubClass<Raw>>(&self) -> Option<T> {
if self.instance_of::<_, T>() {
Ok(unsafe { T::wrap_under_get_rule(self.0 as *const Raw) })
Some(unsafe { T::wrap_under_get_rule(self.0 as *const Raw) })
} else {
Err(CFPropertyListCastError)
None
}
}
}


#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CFPropertyListCastError;

impl fmt::Display for CFPropertyListCastError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ::std::error::Error;
f.write_str(self.description())
}
}

impl ::std::error::Error for CFPropertyListCastError {
fn description(&self) -> &str {
"This CFPropertyList is not an instance of the requested type"
}
}



#[cfg(test)]
pub mod test {
@@ -261,13 +243,13 @@ pub mod test {
fn downcast_string() {
let propertylist = CFString::from_static_string("Bar").to_CFPropertyList();
assert!(propertylist.downcast::<_, CFString>().unwrap().to_string() == "Bar");
assert!(propertylist.downcast::<_, CFBoolean>().is_err());
assert!(propertylist.downcast::<_, CFBoolean>().is_none());
}

#[test]
fn downcast_boolean() {
let propertylist = CFBoolean::true_value().to_CFPropertyList();
assert!(propertylist.downcast::<_, CFBoolean>().is_ok());
assert!(propertylist.downcast::<_, CFString>().is_err());
assert!(propertylist.downcast::<_, CFBoolean>().is_some());
assert!(propertylist.downcast::<_, CFString>().is_none());
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.