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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
use crate::{ContextRoot, SpirvCrossContext};
use std::borrow::Cow;
use std::ffi::{c_char, CStr, CString, NulError};
use std::fmt::{Debug, Display, Formatter};
use std::ops::Deref;
/// An immutable wrapper around a valid UTF-8 string whose memory contents
/// may or may not be originating from a [`SpirvCrossContext`](crate::SpirvCrossContext)
/// context.
///
/// In most cases, users of this library do not need to worry about
/// constructing a [`ContextStr`]. All functions that take strings
/// take `impl Into<ContextStr<'_>>`, which converts automatically from
/// [`&str`](str) and [`String`](String).
///
/// [`ContextStr`] also implements [`Deref`](Deref) for [`&str`](str),
/// so all immutable `str` methods are available.
///
/// # Allocation Behaviour
/// If the string originated from FFI and is a valid nul-terminated
/// C string, then the pointer to the string will be saved,
/// such that when being read by FFI there are no extra allocations
/// required.
///
/// If the provenance of the string is an owned Rust `String`, or
/// a `&str` with lifetime longer than `'a`, then an allocation will
/// occur when passing the string to FFI.
///
/// # Safety
/// Returning `ContextStr<'a>` where `'a` is the lifetime of the
/// [`SpirvCrossContext`](crate::SpirvCrossContext) is **almost always incorrect**.
///
/// The only exception is if the name is explicitly owned by the context,
/// and can not be modified by a `set_`. function.
///
/// In most cases, the returned lifetime should be the lifetime of the mutable borrow,
/// if returning a string from the [`Compiler`].
///
/// [`ContextStr::from_ptr`] takes a context argument, and the context must be
/// the source of provenance for the `ContextStr`.
pub struct ContextStr<'a, T = SpirvCrossContext> {
pointer: Option<ContextPointer<'a, T>>,
cow: Cow<'a, str>,
}
impl<T> Clone for ContextStr<'_, T> {
fn clone(&self) -> Self {
Self {
pointer: self.pointer.clone(),
cow: self.cow.clone(),
}
}
}
pub(crate) struct ContextPointer<'a, T> {
// the lifetime of pointer should be 'a.
pointer: *const c_char,
context: ContextRoot<'a, T>,
}
impl<T> Clone for ContextPointer<'_, T> {
fn clone(&self) -> Self {
Self {
pointer: self.pointer.clone(),
context: self.context.clone(),
}
}
}
impl<'a> Display for ContextStr<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.cow)
}
}
impl<'a> Debug for ContextStr<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.cow)
}
}
pub(crate) enum MaybeOwnedCString<'a, T = SpirvCrossContext> {
Owned(CString),
Borrowed(ContextPointer<'a, T>),
}
impl<T> MaybeOwnedCString<'_, T> {
/// Get a pointer to the C string.
///
/// The pointer will be valid for the lifetime of `self`.
pub fn as_ptr(&self) -> *const c_char {
match self {
MaybeOwnedCString::Owned(c) => c.as_ptr(),
MaybeOwnedCString::Borrowed(ptr) => ptr.pointer,
}
}
}
impl<T> AsRef<str> for ContextStr<'_, T> {
fn as_ref(&self) -> &str {
self.cow.as_ref()
}
}
impl<T> Deref for ContextStr<'_, T> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.cow.deref()
}
}
impl<T> From<String> for ContextStr<'_, T> {
fn from(value: String) -> Self {
Self::from_string(value)
}
}
impl<'a, T> From<&'a str> for ContextStr<'a, T> {
fn from(value: &'a str) -> Self {
Self::from_str(value)
}
}
impl<'a, T> ContextStr<'a, T> {
/// Wraps a raw C string with a safe C string wrapper.
///
/// If the raw C string is valid UTF-8, a pointer to the string will be
/// kept around, which can be passed back to C at zero cost.
///
/// # Safety
///
/// * The memory pointed to by `ptr` must contain a valid nul terminator at the
/// end of the string.
///
/// * `ptr` must be valid for reads of bytes up to and including the nul terminator.
/// This means in particular:
///
/// * The entire memory range of this `CStr` must be contained within a single allocated object!
/// * `ptr` must be non-null even for a zero-length cstr.
///
/// * The memory referenced by the returned `CStr` must not be mutated for
/// the duration of lifetime `'a`.
///
/// * The nul terminator must be within `isize::MAX` from `ptr`
///
/// * The memory pointed to by `ptr` must be valid for the duration of the lifetime `'a`.
///
/// * THe provenance of `context.ptr` must be a superset of `ptr`.
/// # Caveat
///
/// The lifetime for the returned slice is inferred from its usage. To prevent accidental misuse,
/// it's suggested to tie the lifetime to whichever source lifetime is safe in the context,
/// such as by providing a helper function taking the lifetime of a host value for the slice,
/// or by explicit annotation.
pub(crate) unsafe fn from_ptr<'b>(
ptr: *const c_char,
context: ContextRoot<'a, T>,
) -> ContextStr<'b, T>
where
'a: 'b,
{
let cstr = CStr::from_ptr(ptr);
let maybe = cstr.to_string_lossy();
if matches!(&maybe, &Cow::Borrowed(_)) {
Self {
pointer: Some(ContextPointer {
pointer: ptr,
context,
}),
cow: maybe,
}
} else {
Self {
pointer: None,
cow: maybe,
}
}
}
/// Wrap a Rust `&str`.
///
/// This will allocate when exposing to C.
pub(crate) fn from_str(str: &'a str) -> Self {
Self {
pointer: None,
cow: Cow::Borrowed(str),
}
}
/// Wrap a Rust `String`.
///
/// This will allocate when exposing to C.
pub(crate) fn from_string(str: String) -> Self {
Self {
pointer: None,
cow: Cow::Owned(str),
}
}
/// Allocate if necessary, if not then return a pointer to the original cstring.
///
/// The returned pointer will be valid for the lifetime `'a`.
pub(crate) fn to_cstring_ptr(&self) -> Result<MaybeOwnedCString<'a, T>, NulError> {
if let Some(ptr) = &self.pointer {
Ok(MaybeOwnedCString::Borrowed(ptr.clone()))
} else {
let cstring = CString::new(self.cow.to_string())?;
Ok(MaybeOwnedCString::Owned(cstring))
}
}
}
#[cfg(test)]
mod test {
use crate::string::ContextStr;
use crate::ContextRoot;
use std::ffi::CString;
use std::marker::PhantomData;
use std::rc::Rc;
struct LifetimeTest<'a>(PhantomData<&'a ()>);
impl<'a> LifetimeTest<'a> {
pub fn get(self: &Rc<Self>) -> ContextStr<'a, LifetimeTest> {
let cstring = CString::new(String::from("hello"))
.unwrap()
.into_raw()
.cast_const();
unsafe { ContextStr::from_ptr(cstring, ContextRoot::RefCounted(Rc::clone(&self))) }
}
pub fn set(&mut self, cstr: ContextStr<'a, LifetimeTest>) {
println!("{:p}", cstr.to_cstring_ptr().unwrap().as_ptr())
}
}
#[test]
fn test_string() {
// use std::borrow::BorrowMut;
// let mut lt = Rc::new(LifetimeTest(PhantomData));
// let cstr = lt.get();
// lt.borrow_mut().set(cstr)
}
}