use crate::{mem::Alloc, *};
use std::{ffi::CStr, ffi::CString, os::raw::c_char, result};
#[derive(Debug)]
pub struct NngString {
pointer: *mut c_char,
}
impl NngString {
pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<Self> {
let bytes = CString::new(t)?.into_bytes_with_nul();
let bytes = Alloc::new(bytes).ok_or(Error::Errno(NngErrno::ENOMEM))?;
unsafe {
let (ptr, _size) = bytes.take();
Ok(NngString::from_raw(ptr as *mut _))
}
}
pub unsafe fn from_raw(pointer: *mut c_char) -> NngString {
NngString { pointer }
}
pub fn dup(&self) -> NngString {
unsafe { NngString::from_raw(nng_strdup(self.pointer)) }
}
pub fn to_str(&self) -> result::Result<&str, std::str::Utf8Error> {
unsafe { CStr::from_ptr(self.pointer).to_str() }
}
}
impl PartialEq for NngString {
fn eq(&self, other: &NngString) -> bool {
unsafe { CStr::from_ptr(self.pointer) == CStr::from_ptr(other.pointer) }
}
}
impl Clone for NngString {
fn clone(&self) -> Self {
self.dup()
}
}
impl Drop for NngString {
fn drop(&mut self) {
unsafe {
nng_strfree(self.pointer);
}
}
}