Trait ToCString

Source
pub trait ToCString {
    // Required method
    fn into_cstr<'a>(self) -> Cow<'a, CStr>
       where Self: 'a;
}
Expand description

Represents any type that can be represented as a C String. You shouldn’t need to implement this yourself as the most commonly used string-y types already have this implemented.

§Examples

use std::ffi::{CString, CStr};
use std::borrow::Cow;

use zsh_module::ToCString;

let cstr = CStr::from_bytes_with_nul(b"Hello, world!\0").unwrap();
let cstring = CString::new("Hello, world!").unwrap();

assert!(matches!(cstr.into_cstr(), Cow::Borrowed(data) if data == cstr));

let string = "Hello, world!";
assert!(matches!(ToCString::into_cstr(string), Cow::Owned(cstring)));

Required Methods§

Source

fn into_cstr<'a>(self) -> Cow<'a, CStr>
where Self: 'a,

Implementations on Foreign Types§

Source§

impl ToCString for &str

Source§

fn into_cstr<'a>(self) -> Cow<'a, CStr>
where Self: 'a,

Source§

impl ToCString for &CStr

Source§

fn into_cstr<'a>(self) -> Cow<'a, CStr>
where Self: 'a,

Source§

impl ToCString for &[u8]

Source§

fn into_cstr<'a>(self) -> Cow<'a, CStr>
where Self: 'a,

Source§

impl ToCString for *const c_char

Source§

fn into_cstr<'a>(self) -> Cow<'a, CStr>

Source§

impl ToCString for *mut c_char

Source§

fn into_cstr<'a>(self) -> Cow<'a, CStr>

Source§

impl ToCString for CString

Source§

fn into_cstr<'a>(self) -> Cow<'a, CStr>

Source§

impl ToCString for String

Source§

fn into_cstr<'a>(self) -> Cow<'a, CStr>
where Self: 'a,

Source§

impl ToCString for Vec<u8>

Source§

fn into_cstr<'a>(self) -> Cow<'a, CStr>
where Self: 'a,

Implementors§