AsLtPtr

Trait AsLtPtr 

Source
pub trait AsLtPtr {
    type Target: ?Sized;

    // Required method
    fn as_lt_ptr<'a>(&'a self) -> ConstLtPtr<'a, Self::Target>;
}
Expand description

Trait for conversion into a ConstLtPtr.

Required Associated Types§

Required Methods§

Source

fn as_lt_ptr<'a>(&'a self) -> ConstLtPtr<'a, Self::Target>

Returns a pointer as if by as_ptr on a type that implements this, but with a bound lifetime.

Implementations on Foreign Types§

Source§

impl AsLtPtr for CStr

Conversion from CStr to ConstLtPtr.

Source§

type Target = i8

The target type of this conversion.

Source§

fn as_lt_ptr(&self) -> ConstLtPtr<'_, c_char>

Returns the inner pointer to this C string.

The returned pointer points to a contiguous region of memory terminated with a 0 byte to represent the end of the string.

§Examples

ltptr protects you from dangling pointers: (adapted from CStr::as_ptr)

use std::ffi::CString;
use ltptr::AsLtPtr;

let ptr = CString::new("Hello").unwrap().as_lt_ptr();
unsafe {
    // would be dangling but the compiler prevents this from compiling
    ptr.as_raw();
}

Which you can fix by adding a binding:

use std::ffi::CString;
use ltptr::AsLtPtr;

let hello = CString::new("Hello").unwrap();
let ptr = hello.as_lt_ptr();
unsafe {
    // perfectly fine
    ptr.as_raw();
}
Source§

impl<T> AsLtPtr for [T]

Source§

type Target = T

Source§

fn as_lt_ptr(&self) -> ConstLtPtr<'_, T>

Implementors§