mlx_c_rust/types/
string.rs1use std::ffi::{CStr, CString};
4
5use crate::MlxCError;
6use crate::error::check_status;
7use crate::raw;
8
9#[derive(Debug)]
11pub struct MlxString(raw::mlx_string);
12
13impl MlxString {
14 pub fn empty() -> Self {
16 let raw_string = unsafe { raw::mlx_string_new() };
19 Self(raw_string)
20 }
21
22 pub fn set(&mut self, source: &Self) -> Result<(), MlxCError> {
24 let status = unsafe { raw::mlx_string_set(&mut self.0, source.0) };
27 check_status(status, "copy an MLX string")
28 }
29
30 #[must_use]
32 pub fn to_string_lossy(&self) -> String {
33 let data_pointer = unsafe { raw::mlx_string_data(self.0) };
36 if data_pointer.is_null() {
37 return String::new();
38 }
39 unsafe { CStr::from_ptr(data_pointer) }
42 .to_string_lossy()
43 .into_owned()
44 }
45
46 pub(crate) fn raw_mut(&mut self) -> *mut raw::mlx_string {
47 &mut self.0
48 }
49
50 pub(crate) fn require_populated(&self, operation: &'static str) -> Result<(), MlxCError> {
51 if self.0.ctx.is_null() {
52 return Err(MlxCError {
53 operation,
54 description: "MLX returned an empty string handle".to_owned(),
55 });
56 }
57 Ok(())
58 }
59}
60
61impl Drop for MlxString {
62 fn drop(&mut self) {
63 unsafe {
66 raw::mlx_string_free(self.0);
67 }
68 }
69}
70
71impl std::str::FromStr for MlxString {
74 type Err = MlxCError;
75
76 fn from_str(value: &str) -> Result<Self, Self::Err> {
77 let value_argument = CString::new(value).map_err(|_| MlxCError {
78 operation: "create an MLX string",
79 description: "string contains an interior null byte".to_owned(),
80 })?;
81 let raw_string = unsafe { raw::mlx_string_new_data(value_argument.as_ptr()) };
84 let string = Self(raw_string);
85 string.require_populated("create an MLX string")?;
86 Ok(string)
87 }
88}