Skip to content

Commit 1c65ff6

Browse files
committed
Implement AsRef, AsMut, Display and FromStr on Il2CppString
1 parent ee13b6f commit 1c65ff6

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/system.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{marker::PhantomData, ops::{Deref, DerefMut}};
1+
use std::{fmt::{Display, Formatter}, marker::PhantomData, ops::{Deref, DerefMut}, str::FromStr};
22

3-
use crate::prelude::{Il2CppArray, Il2CppClass, Il2CppClassData, Il2CppObject, MethodInfo};
3+
use crate::prelude::{Il2CppArray, Il2CppClass, Il2CppClassData, Il2CppObject, MethodInfo, OptionalMethod};
44

55
/// A type alias for `Il2CppObject<SystemString>`.
66
///
@@ -97,6 +97,24 @@ impl Il2CppString {
9797
}
9898
}
9999

100+
impl AsRef<[u16]> for &'_ Il2CppString {
101+
fn as_ref(&self) -> &[u16] {
102+
unsafe { std::slice::from_raw_parts(self.string.as_ptr(), self.len as _) }
103+
}
104+
}
105+
106+
impl AsMut<[u16]> for &'_ mut Il2CppString {
107+
fn as_mut(&mut self) -> &mut [u16] {
108+
unsafe { std::slice::from_raw_parts_mut(self.string.as_mut_ptr(), self.len as _) }
109+
}
110+
}
111+
112+
impl Display for Il2CppString {
113+
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
114+
write!(f, "{}", self.to_string())
115+
}
116+
}
117+
100118
impl<T: AsRef<str>> From<T> for &'_ Il2CppString {
101119
fn from(value: T) -> Self {
102120
Il2CppString::new(value)
@@ -105,7 +123,15 @@ impl<T: AsRef<str>> From<T> for &'_ Il2CppString {
105123

106124
impl PartialEq for Il2CppString {
107125
fn eq(&self, other: &Self) -> bool {
108-
unsafe { system_string_equals(self, other) }
126+
unsafe { system_string_equals(self, other, None) }
127+
}
128+
}
129+
130+
impl FromStr for &'_ Il2CppString {
131+
type Err = ();
132+
133+
fn from_str(s: &str) -> Result<Self, Self::Err> {
134+
Ok(Il2CppString::new(s))
109135
}
110136
}
111137

0 commit comments

Comments
 (0)