Avoid need to cast away const in the C caller of zip339_free_phrase.

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Hopwood 2021-08-20 02:41:56 +01:00
parent ac54e56665
commit fc3ea09b89
2 changed files with 5 additions and 4 deletions

View File

@ -43,7 +43,7 @@ extern "C" {
const char * zip339_entropy_to_phrase(Language language, const uint8_t *entropy, size_t entropy_len);
/// Frees a phrase returned by `zip339_entropy_to_phrase`.
void zip339_free_phrase(char *phrase);
void zip339_free_phrase(const char *phrase);
/// Returns `true` if the given string is a valid mnemonic phrase in the given `Language`.
bool zip339_validate_phrase(Language language, const char *phrase);

View File

@ -50,7 +50,7 @@ pub extern "C" fn zip339_entropy_to_phrase(
let entropy = unsafe { slice::from_raw_parts(entropy, entropy_len) }.to_vec();
if let Ok(mnemonic) = zip339::Mnemonic::from_entropy_in(language, entropy) {
if let Ok(phrase) = CString::new(mnemonic.phrase()) {
return phrase.into_raw();
return phrase.into_raw() as *const c_char;
}
}
}
@ -59,10 +59,11 @@ pub extern "C" fn zip339_entropy_to_phrase(
/// Frees a phrase returned by `zip339_entropy_to_phrase`.
#[no_mangle]
pub extern "C" fn zip339_free_phrase(phrase: *mut c_char) {
pub extern "C" fn zip339_free_phrase(phrase: *const c_char) {
if !phrase.is_null() {
unsafe {
CString::from_raw(phrase);
// It is correct to cast away const here; the memory is not actually immutable.
CString::from_raw(phrase as *mut c_char);
}
}
}