Add .strdup() method to Option<AsRef<str>>

We already have a .strdup() method on AsRef<str>, this adds this
method also to an option of this.  In case the option is None a NULL
pointer is returned.

This is done by using a new trait, as the type system otherwise
considers such an implementation conflicting with the existing one.
This commit is contained in:
Floris Bruynooghe
2020-02-13 20:47:32 +01:00
committed by holger krekel
parent 2977ceb459
commit e2f1ea1444
2 changed files with 48 additions and 10 deletions

View File

@@ -3227,7 +3227,7 @@ pub unsafe extern "C" fn dc_lot_get_text1(lot: *mut dc_lot_t) -> *mut libc::c_ch
}
let lot = &*lot;
strdup_opt(lot.get_text1())
lot.get_text1().strdup()
}
#[no_mangle]
@@ -3238,7 +3238,7 @@ pub unsafe extern "C" fn dc_lot_get_text2(lot: *mut dc_lot_t) -> *mut libc::c_ch
}
let lot = &*lot;
strdup_opt(lot.get_text2())
lot.get_text2().strdup()
}
#[no_mangle]
@@ -3323,13 +3323,6 @@ impl<T: Default, E: std::fmt::Display> ResultExt<T, E> for Result<T, E> {
}
}
unsafe fn strdup_opt(s: Option<impl AsRef<str>>) -> *mut libc::c_char {
match s {
Some(s) => s.as_ref().strdup(),
None => ptr::null_mut(),
}
}
trait ResultNullableExt<T> {
fn into_raw(self) -> *mut T;
}