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

Add proper error for CFPropertyList::downcast

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

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

use libc::c_void;

@@ -178,16 +179,34 @@ impl CFPropertyList {
/// // Cast it down again.
/// assert!(propertylist.downcast::<_, CFString>().unwrap().to_string() == "FooBar");
/// ```
pub fn downcast<Raw, T: CFPropertyListSubClass<Raw>>(&self) -> Result<T, ()> {
pub fn downcast<Raw, T: CFPropertyListSubClass<Raw>>(&self) -> Result<T, CFPropertyListCastError> {
if self.instance_of::<_, T>() {
Ok(unsafe { T::wrap_under_get_rule(self.0 as *const Raw) })
} else {
Err(())
Err(CFPropertyListCastError)
}
}
}


#[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 {
use super::*;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.