remove use of deprecated item 'try': use the ? operator instead

Signed-off-by: Nikola Pajkovsky <nikola.pajkovsky@livesporttv.cz>
This commit is contained in:
Nikola Pajkovsky 2020-05-21 12:11:15 +02:00
parent 415ad30bd0
commit 51b2cac594
5 changed files with 33 additions and 34 deletions

View file

@ -69,8 +69,7 @@ pub fn magick_wand_terminus() {
pub fn magick_query_fonts(pattern: &str) -> Result<Vec<String>, &'static str> { pub fn magick_query_fonts(pattern: &str) -> Result<Vec<String>, &'static str> {
let mut number_fonts: size_t = 0; let mut number_fonts: size_t = 0;
let c_string = let c_string = ::std::ffi::CString::new(pattern).map_err(|_| "could not convert to cstring")?;
try!(::std::ffi::CString::new(pattern).map_err(|_| "could not convert to cstring"));
let ptr = let ptr =
unsafe { bindings::MagickQueryFonts(c_string.as_ptr(), &mut number_fonts as *mut size_t) }; unsafe { bindings::MagickQueryFonts(c_string.as_ptr(), &mut number_fonts as *mut size_t) };
if ptr.is_null() { if ptr.is_null() {

View file

@ -35,7 +35,7 @@ wand_common!(
impl DrawingWand { impl DrawingWand {
pub fn draw_annotation(&mut self, x: f64, y: f64, text: &str) -> Result<(), &'static str> { pub fn draw_annotation(&mut self, x: f64, y: f64, text: &str) -> Result<(), &'static str> {
let c_string = try!(CString::new(text).map_err(|_| "could not convert to cstring")); let c_string = CString::new(text).map_err(|_| "could not convert to cstring")?;
unsafe { bindings::DrawAnnotation(self.wand, x, y, c_string.as_ptr() as *const _) }; unsafe { bindings::DrawAnnotation(self.wand, x, y, c_string.as_ptr() as *const _) };
Ok(()) Ok(())
} }
@ -94,13 +94,13 @@ impl DrawingWand {
impl fmt::Debug for DrawingWand { impl fmt::Debug for DrawingWand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(writeln!(f, "DrawingWand {{")); writeln!(f, "DrawingWand {{")?;
try!(writeln!(f, " Exception: {:?}", self.get_exception())); writeln!(f, " Exception: {:?}", self.get_exception())?;
try!(writeln!(f, " IsWand: {:?}", self.is_wand())); writeln!(f, " IsWand: {:?}", self.is_wand())?;
try!(self.fmt_unchecked_settings(f, " ")); self.fmt_unchecked_settings(f, " ")?;
try!(self.fmt_string_settings(f, " ")); self.fmt_string_settings(f, " ")?;
try!(self.fmt_string_unchecked_settings(f, " ")); self.fmt_string_unchecked_settings(f, " ")?;
try!(self.fmt_pixel_settings(f, " ")); self.fmt_pixel_settings(f, " ")?;
writeln!(f, "}}") writeln!(f, "}}")
} }
} }

View file

@ -118,7 +118,7 @@ macro_rules! set_get {
} }
)* )*
pub fn fmt_checked_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result { pub fn fmt_checked_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result {
$( try!(writeln!(f, "{}{:<50}: {:?}", prefix, stringify!($c_get), self.$get())); )* $( writeln!(f, "{}{:<50}: {:?}", prefix, stringify!($c_get), self.$get())?; )*
Ok(()) Ok(())
} }
} }
@ -135,7 +135,7 @@ macro_rules! set_get_unchecked {
} }
)* )*
pub fn fmt_unchecked_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result { pub fn fmt_unchecked_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result {
$( try!(writeln!(f, "{}{:<50}: {:?}", prefix, stringify!($c_get), self.$get())); )* $( writeln!(f, "{}{:<50}: {:?}", prefix, stringify!($c_get), self.$get()); )*
Ok(()) Ok(())
} }
} }
@ -154,7 +154,7 @@ macro_rules! string_get {
Ok(result) Ok(result)
} }
} }
} };
} }
macro_rules! string_set_get { macro_rules! string_set_get {
@ -162,7 +162,7 @@ macro_rules! string_set_get {
$( $(
string_get!($get, $c_get); string_get!($get, $c_get);
pub fn $set(&mut self, s: &str) -> Result<(), &'static str> { pub fn $set(&mut self, s: &str) -> Result<(), &'static str> {
let c_string = try!(::std::ffi::CString::new(s).map_err(|_| "could not convert to cstring")); let c_string = std::ffi::CString::new(s).map_err(|_| "could not convert to cstring")?;
match unsafe { ::bindings::$c_set(self.wand, c_string.as_ptr()) } { match unsafe { ::bindings::$c_set(self.wand, c_string.as_ptr()) } {
::bindings::MagickBooleanType_MagickTrue => Ok(()), ::bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err(concat!(stringify!($set), " returned false")) _ => Err(concat!(stringify!($set), " returned false"))
@ -170,7 +170,7 @@ macro_rules! string_set_get {
} }
)* )*
pub fn fmt_string_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result { pub fn fmt_string_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result {
$( try!(writeln!(f, "{}{:<50}: {:?}", prefix, stringify!($c_get), self.$get())); )* $( writeln!(f, "{}{:<50}: {:?}", prefix, stringify!($c_get), self.$get())?; )*
Ok(()) Ok(())
} }
} }
@ -181,13 +181,13 @@ macro_rules! string_set_get_unchecked {
$( $(
string_get!($get, $c_get); string_get!($get, $c_get);
pub fn $set(&mut self, s: &str) -> Result<(), &'static str> { pub fn $set(&mut self, s: &str) -> Result<(), &'static str> {
let c_string = try!(::std::ffi::CString::new(s).map_err(|_| "could not convert to cstring")); let c_string = ::std::ffi::CString::new(s).map_err(|_| "could not convert to cstring")?;
unsafe { ::bindings::$c_set(self.wand, c_string.as_ptr()) }; unsafe { ::bindings::$c_set(self.wand, c_string.as_ptr()) };
Ok(()) Ok(())
} }
)* )*
pub fn fmt_string_unchecked_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result { pub fn fmt_string_unchecked_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result {
$( try!(writeln!(f, "{}{:<50}: {:?}", prefix, stringify!($c_get), self.$get())); )* $( writeln!(f, "{}{:<50}: {:?}", prefix, stringify!($c_get), self.$get())?; )*
Ok(()) Ok(())
} }
} }
@ -207,8 +207,8 @@ macro_rules! pixel_set_get {
)* )*
pub fn fmt_pixel_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result { pub fn fmt_pixel_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result {
$( $(
try!(writeln!(f, "{}{:<50}: ", prefix, stringify!($c_get))); writeln!(f, "{}{:<50}: ", prefix, stringify!($c_get))?;
try!(self.$get().fmt_w_prefix(f, &format!("{}{:<53}", prefix, " ") )); self.$get().fmt_w_prefix(f, &format!("{}{:<53}", prefix, " ") )?;
)* )*
Ok(()) Ok(())
} }
@ -235,14 +235,14 @@ macro_rules! color_set_get {
} }
)* )*
pub fn fmt_color_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result { pub fn fmt_color_settings(&self, f: &mut ::std::fmt::Formatter, prefix: &str) -> ::std::fmt::Result {
try!(writeln!(f, "{}Color: {:?}, normalized: {:?}\n{}hsl: {:?}", writeln!(f, "{}Color: {:?}, normalized: {:?}\n{}hsl: {:?}",
prefix, prefix,
self.get_color_as_string(), self.get_color_as_string(),
self.get_color_as_normalized_string(), self.get_color_as_normalized_string(),
prefix, prefix,
self.get_hsl() self.get_hsl()
)); )?;
$( try!(writeln!(f, "{}{:<10}: {:>} quantum: {}", prefix, stringify!($c_get).split_at(8).1, self.$get(), self.$get_quantum())); )* $( writeln!(f, "{}{:<10}: {:>} quantum: {}", prefix, stringify!($c_get).split_at(8).1, self.$get(), self.$get_quantum())?; )*
Ok(()) Ok(())
} }
} }

View file

@ -75,7 +75,7 @@ impl MagickWand {
angle: f64, angle: f64,
text: &str, text: &str,
) -> Result<(), &'static str> { ) -> Result<(), &'static str> {
let c_string = try!(CString::new(text).map_err(|_| "could not convert to cstring")); let c_string = CString::new(text).map_err(|_| "could not convert to cstring")?;
match unsafe { match unsafe {
bindings::MagickAnnotateImage( bindings::MagickAnnotateImage(
self.wand, self.wand,
@ -841,11 +841,11 @@ impl MagickWand {
impl fmt::Debug for MagickWand { impl fmt::Debug for MagickWand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(writeln!(f, "MagickWand {{")); writeln!(f, "MagickWand {{")?;
try!(writeln!(f, " Exception: {:?}", self.get_exception())); writeln!(f, " Exception: {:?}", self.get_exception())?;
try!(writeln!(f, " IsWand: {:?}", self.is_wand())); writeln!(f, " IsWand: {:?}", self.is_wand())?;
try!(self.fmt_string_settings(f, " ")); self.fmt_string_settings(f, " ")?;
try!(self.fmt_checked_settings(f, " ")); self.fmt_checked_settings(f, " ")?;
writeln!(f, "}}") writeln!(f, "}}")
} }
} }

View file

@ -70,16 +70,16 @@ impl PixelWand {
pub fn fmt_w_prefix(&self, f: &mut fmt::Formatter, prefix: &str) -> fmt::Result { pub fn fmt_w_prefix(&self, f: &mut fmt::Formatter, prefix: &str) -> fmt::Result {
let mut prf = prefix.to_string(); let mut prf = prefix.to_string();
prf.push_str(" "); prf.push_str(" ");
try!(writeln!(f, "{}PixelWand {{", prefix)); writeln!(f, "{}PixelWand {{", prefix)?;
try!(writeln!(f, "{}Exception: {:?}", prf, self.get_exception())); writeln!(f, "{}Exception: {:?}", prf, self.get_exception())?;
try!(writeln!(f, "{}IsWand: {:?}", prf, self.is_wand())); writeln!(f, "{}IsWand: {:?}", prf, self.is_wand())?;
try!(self.fmt_unchecked_settings(f, &prf)); self.fmt_unchecked_settings(f, &prf)?;
try!(self.fmt_color_settings(f, &prf)); self.fmt_color_settings(f, &prf)?;
writeln!(f, "{}}}", prefix) writeln!(f, "{}}}", prefix)
} }
pub fn set_color(&mut self, s: &str) -> Result<(), &'static str> { pub fn set_color(&mut self, s: &str) -> Result<(), &'static str> {
let c_string = try!(CString::new(s).map_err(|_| "could not convert to cstring")); let c_string = CString::new(s).map_err(|_| "could not convert to cstring")?;
match unsafe { bindings::PixelSetColor(self.wand, c_string.as_ptr()) } { match unsafe { bindings::PixelSetColor(self.wand, c_string.as_ptr()) } {
bindings::MagickBooleanType_MagickTrue => Ok(()), bindings::MagickBooleanType_MagickTrue => Ok(()),
_ => Err("failed to set color"), _ => Err("failed to set color"),