diff options
-rw-r--r-- | src/codegen/mod.rs | 12 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-3.9/objc_inheritance.rs | 22 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-4/objc_inheritance.rs | 21 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-5/objc_inheritance.rs | 19 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-9/objc_inheritance.rs | 21 | ||||
-rw-r--r-- | tests/expectations/tests/objc_protocol_inheritance.rs | 7 |
6 files changed, 30 insertions, 72 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 0039a859..d07e27a7 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -49,7 +49,6 @@ use crate::{Entry, HashMap, HashSet}; use std; use std::borrow::Cow; use std::cell::Cell; -use std::collections::HashSet as CollectionHashSet; use std::collections::VecDeque; use std::fmt::Write; use std::iter; @@ -3922,8 +3921,7 @@ impl CodeGenerator for ObjCInterface { } }; result.push(struct_block); - let mut protocol_set: CollectionHashSet<ItemId> = - CollectionHashSet::new(); + let mut protocol_set: HashSet<ItemId> = Default::default(); for protocol_id in self.conforms_to.iter() { protocol_set.insert(*protocol_id); let protocol_name = ctx.rust_ident( @@ -3967,8 +3965,7 @@ impl CodeGenerator for ObjCInterface { }; result.push(impl_trait); for protocol_id in parent.conforms_to.iter() { - if !protocol_set.contains(protocol_id) { - protocol_set.insert(*protocol_id); + if protocol_set.insert(*protocol_id) { let protocol_name = ctx.rust_ident( ctx.resolve_type( protocol_id.expect_type_id(ctx), @@ -3995,15 +3992,16 @@ impl CodeGenerator for ObjCInterface { }; result.push(from_block); + let error_msg = format!("This {} cannot be downcasted to {}", parent_struct_name, child_struct_name); let try_into_block = quote! { impl std::convert::TryFrom<#parent_struct> for #class_name { - type Error = String; + type Error = &'static str; fn try_from(parent: #parent_struct) -> Result<#class_name, Self::Error> { let is_kind_of : bool = unsafe { msg_send!(parent, isKindOfClass:class!(#class_name))}; if is_kind_of { Ok(#class_name(parent.0)) } else { - Err(format!("This {} is not an cannot be downcasted to {}", #parent_struct_name, #child_struct_name)) + Err(#error_msg) } } } diff --git a/tests/expectations/tests/libclang-3.9/objc_inheritance.rs b/tests/expectations/tests/libclang-3.9/objc_inheritance.rs index 76b758ec..f2049a88 100644 --- a/tests/expectations/tests/libclang-3.9/objc_inheritance.rs +++ b/tests/expectations/tests/libclang-3.9/objc_inheritance.rs @@ -50,17 +50,14 @@ impl From<Bar> for Foo { } } impl std::convert::TryFrom<Foo> for Bar { - type Error = String; + type Error = &'static str; fn try_from(parent: Foo) -> Result<Bar, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Bar)) }; if is_kind_of { Ok(Bar(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Bar" - )) + Err("This Foo cannot be downcasted to Bar") } } } @@ -70,19 +67,15 @@ impl From<Baz> for Bar { Bar(child.0) } } - impl std::convert::TryFrom<Bar> for Baz { - type Error = String; + type Error = &'static str; fn try_from(parent: Bar) -> Result<Baz, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Baz)) }; if is_kind_of { Ok(Baz(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Bar", "Baz" - )) + Err("This Bar cannot be downcasted to Baz") } } } @@ -93,17 +86,14 @@ impl From<Baz> for Foo { } } impl std::convert::TryFrom<Foo> for Baz { - type Error = String; + type Error = &'static str; fn try_from(parent: Foo) -> Result<Baz, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Baz)) }; if is_kind_of { Ok(Baz(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Baz" - )) + Err("This Foo cannot be downcasted to Baz") } } } diff --git a/tests/expectations/tests/libclang-4/objc_inheritance.rs b/tests/expectations/tests/libclang-4/objc_inheritance.rs index a84f6f37..5f07dbaa 100644 --- a/tests/expectations/tests/libclang-4/objc_inheritance.rs +++ b/tests/expectations/tests/libclang-4/objc_inheritance.rs @@ -49,17 +49,14 @@ impl From<Bar> for Foo { } } impl std::convert::TryFrom<Foo> for Bar { - type Error = String; + type Error = &'static str; fn try_from(parent: Foo) -> Result<Bar, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Bar)) }; if is_kind_of { Ok(Bar(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Bar" - )) + Err("This Foo cannot be downcasted to Bar") } } } @@ -87,17 +84,14 @@ impl From<Baz> for Bar { } } impl std::convert::TryFrom<Bar> for Baz { - type Error = String; + type Error = &'static str; fn try_from(parent: Bar) -> Result<Baz, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Baz)) }; if is_kind_of { Ok(Baz(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Bar", "Baz" - )) + Err("This Bar cannot be downcasted to Baz") } } } @@ -108,17 +102,14 @@ impl From<Baz> for Foo { } } impl std::convert::TryFrom<Foo> for Baz { - type Error = String; + type Error = &'static str; fn try_from(parent: Foo) -> Result<Baz, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Baz)) }; if is_kind_of { Ok(Baz(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Baz" - )) + Err("This Foo cannot be downcasted to Baz") } } } diff --git a/tests/expectations/tests/libclang-5/objc_inheritance.rs b/tests/expectations/tests/libclang-5/objc_inheritance.rs index a84f6f37..42c961e7 100644 --- a/tests/expectations/tests/libclang-5/objc_inheritance.rs +++ b/tests/expectations/tests/libclang-5/objc_inheritance.rs @@ -56,10 +56,7 @@ impl std::convert::TryFrom<Foo> for Bar { if is_kind_of { Ok(Bar(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Bar" - )) + Err("This Foo cannot be downcasted to Bar") } } } @@ -87,17 +84,14 @@ impl From<Baz> for Bar { } } impl std::convert::TryFrom<Bar> for Baz { - type Error = String; + type Error = &'static str; fn try_from(parent: Bar) -> Result<Baz, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Baz)) }; if is_kind_of { Ok(Baz(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Bar", "Baz" - )) + Err("This Bar cannot be downcasted to Baz") } } } @@ -108,17 +102,14 @@ impl From<Baz> for Foo { } } impl std::convert::TryFrom<Foo> for Baz { - type Error = String; + type Error = &'static str; fn try_from(parent: Foo) -> Result<Baz, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Baz)) }; if is_kind_of { Ok(Baz(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Baz" - )) + Err("This Foo cannot be downcasted to Baz") } } } diff --git a/tests/expectations/tests/libclang-9/objc_inheritance.rs b/tests/expectations/tests/libclang-9/objc_inheritance.rs index a84f6f37..5f07dbaa 100644 --- a/tests/expectations/tests/libclang-9/objc_inheritance.rs +++ b/tests/expectations/tests/libclang-9/objc_inheritance.rs @@ -49,17 +49,14 @@ impl From<Bar> for Foo { } } impl std::convert::TryFrom<Foo> for Bar { - type Error = String; + type Error = &'static str; fn try_from(parent: Foo) -> Result<Bar, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Bar)) }; if is_kind_of { Ok(Bar(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Bar" - )) + Err("This Foo cannot be downcasted to Bar") } } } @@ -87,17 +84,14 @@ impl From<Baz> for Bar { } } impl std::convert::TryFrom<Bar> for Baz { - type Error = String; + type Error = &'static str; fn try_from(parent: Bar) -> Result<Baz, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Baz)) }; if is_kind_of { Ok(Baz(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Bar", "Baz" - )) + Err("This Bar cannot be downcasted to Baz") } } } @@ -108,17 +102,14 @@ impl From<Baz> for Foo { } } impl std::convert::TryFrom<Foo> for Baz { - type Error = String; + type Error = &'static str; fn try_from(parent: Foo) -> Result<Baz, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Baz)) }; if is_kind_of { Ok(Baz(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Baz" - )) + Err("This Foo cannot be downcasted to Baz") } } } diff --git a/tests/expectations/tests/objc_protocol_inheritance.rs b/tests/expectations/tests/objc_protocol_inheritance.rs index 14570e1e..598273ba 100644 --- a/tests/expectations/tests/objc_protocol_inheritance.rs +++ b/tests/expectations/tests/objc_protocol_inheritance.rs @@ -52,17 +52,14 @@ impl From<Bar> for Foo { } } impl std::convert::TryFrom<Foo> for Bar { - type Error = String; + type Error = &'static str; fn try_from(parent: Foo) -> Result<Bar, Self::Error> { let is_kind_of: bool = unsafe { msg_send!(parent, isKindOfClass: class!(Bar)) }; if is_kind_of { Ok(Bar(parent.0)) } else { - Err(format!( - "This {} is not an cannot be downcasted to {}", - "Foo", "Bar" - )) + Err("This Foo cannot be downcasted to Bar") } } } |