This repository was archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.rs
87 lines (73 loc) · 1.92 KB
/
helpers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Citadel C bindings library (libcitadel)
// Written in 2020 by
// Dr. Maxim Orlovsky <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
// TODO: Consider moving to rust-amplify library
use libc::c_char;
use std::ffi::{CStr, CString};
pub trait TryIntoRaw {
fn try_into_raw(self) -> Option<*const c_char>;
}
pub trait TryFromRaw {
fn try_from_raw(ptr: *mut c_char) -> Option<Self>
where
Self: Sized;
}
impl TryIntoRaw for String {
fn try_into_raw(self) -> Option<*const c_char> {
CString::new(self)
.map(CString::into_raw)
.map(|ptr| ptr as *const c_char)
.ok()
}
}
impl TryFromRaw for String {
fn try_from_raw(ptr: *mut c_char) -> Option<String> {
unsafe { CString::from_raw(ptr) }.into_string().ok()
}
}
pub trait TryAsStr {
fn try_as_str(self) -> Option<&'static str>;
}
pub trait TryIntoString {
fn try_into_string(self) -> Option<String>;
}
impl TryAsStr for *const c_char {
fn try_as_str(self: *const c_char) -> Option<&'static str> {
if self.is_null() {
return None;
}
unsafe { CStr::from_ptr(self) }.to_str().ok()
}
}
impl TryIntoString for *mut c_char {
fn try_into_string(self: *mut c_char) -> Option<String> {
if self.is_null() {
return None;
}
String::try_from_raw(self)
}
}
/*
impl Try for *const c_char {
type Ok = *const c_char;
type Error = ();
fn into_result(self) -> Result<Self::Ok, Self::Error> {
if self.is_null() {
Err(())
} else {
Ok(self)
}
}
fn from_error(v: Self::Error) -> Self {
std::ptr::null()
}
fn from_ok(v: Self::Ok) -> Self {
v
}
}
*/