From 4bf2fc18e55ac0908ba9027ce2487cfc4e383f98 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Tue, 4 Feb 2020 22:46:22 +0100 Subject: [PATCH] Write with_inner() in function of try_inner() I guess this is a little neater. Note that this does change all the existing error logging to warning logging. I believe this is correct as these error log messages are really programming errors rather than errors which need to be show to the user. Also chose to make entire with_inner unsafe, there is little to be gained from hiding the unsafe as this is only used from unsafe functions in the first place. --- deltachat-ffi/src/lib.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 8ba22d32c..c81a361ab 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -118,18 +118,17 @@ impl ContextWrapper { /// the appropriate return value for an error return since this /// differs for various functions on the FFI API: sometimes 0, /// NULL, an empty string etc. - fn with_inner(&self, ctxfn: F) -> Result + /// + /// Prefer to use [ContextWrapper::try_inner], we might want to + /// remove this function at some point to reduce the cognitive + /// overload of having two functions which are too similar. + unsafe fn with_inner(&self, ctxfn: F) -> Result where F: FnOnce(&Context) -> T, { - let guard = self.inner.read().unwrap(); - match guard.as_ref() { - Some(ref ctx) => Ok(ctxfn(ctx)), - None => { - unsafe { self.error("context not open") }; - Err(()) - } - } + self.try_inner(|ctx| Ok(ctxfn(ctx))).map_err(|err| { + self.warning(&err.to_string()); + }) } /// Unlock the context and execute a closure with it.