CStrHeap

Struct CStrHeap 

Source
pub struct CStrHeap { /* private fields */ }
Expand description

A heap-allocated C-compatible string wrapper.

CStrHeap owns a CString internally, and implements the CStrLike trait to provide a uniform interface with stack-allocated alternatives (e.g. CStrStack).

This is used as a fallback when the string cannot fit into a fixed-size stack buffer.

Implementations§

Source§

impl CStrHeap

Source

pub fn new(s: CString) -> Self

Creates a new CStrHeap from a given CString.

§Arguments
  • s - A CString instance to be wrapped.
§Examples
use std::ffi::CString;
use stack_cstr::CStrHeap;

let cstr = CString::new("hello").unwrap();
let heap = CStrHeap::new(cstr);
assert_eq!(unsafe { std::ffi::CStr::from_ptr(heap.as_ptr()) }.to_str().unwrap(), "hello");
Source

pub fn as_ptr(&self) -> *const c_char

Returns a raw pointer to the underlying C string.

The pointer is guaranteed to be valid as long as the CStrHeap instance is alive. The string is null-terminated.

Source

pub fn as_cstr(&self) -> &CStr

Returns a reference to the underlying CStr.

§Examples
use std::ffi::CString;
use stack_cstr::CStrHeap;

let cstr = CString::new("hello").unwrap();
let heap = CStrHeap::new(cstr);
let slice = heap.as_cstr();
assert_eq!(slice.to_str().unwrap(), "hello");

Trait Implementations§

Source§

impl CStrLike for CStrHeap

Source§

fn as_ptr(&self) -> *const c_char

Returns a raw pointer to the null-terminated C string. Read more
Source§

fn as_cstr(&self) -> &CStr

Returns a reference to the underlying CStr.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.