@@ -88,6 +88,12 @@ impl GString {
8888 ///
8989 /// Note: This operation is *O*(*n*). Consider using [`chars_unchecked`][Self::chars_unchecked]
9090 /// if you can make sure the string is a valid UTF-32.
91+ #[ cfg_attr(
92+ since_api = "4.1" ,
93+ deprecated = "Use `chars()` instead. \n \
94+ Since version 4.1, Godot ensures valid UTF-32, checked and unchecked overloads are no longer needed. \n \
95+ For details, see [godotengine/godot#74760](https://github.com/godotengine/godot/pull/74760)."
96+ ) ]
9197 pub fn chars_checked ( & self ) -> & [ char ] {
9298 unsafe {
9399 let s = self . string_sys ( ) ;
@@ -111,6 +117,12 @@ impl GString {
111117 /// Make sure the string only contains valid unicode scalar values, currently
112118 /// Godot allows for unpaired surrogates and out of range code points to be appended
113119 /// into the string.
120+ #[ cfg_attr(
121+ since_api = "4.1" ,
122+ deprecated = "Use `chars()` instead. \n \
123+ Since version 4.1, ensures valid UTF-32, checked and unchecked overloads are no longer needed. \n \
124+ For details, see [godotengine/godot#74760](https://github.com/godotengine/godot/pull/74760)."
125+ ) ]
114126 pub unsafe fn chars_unchecked ( & self ) -> & [ char ] {
115127 let s = self . string_sys ( ) ;
116128 let len = interface_fn ! ( string_to_utf32_chars) ( s, std:: ptr:: null_mut ( ) , 0 ) ;
@@ -123,6 +135,16 @@ impl GString {
123135 std:: slice:: from_raw_parts ( ptr as * const char , len as usize )
124136 }
125137
138+ /// Gets the internal chars slice from a [`GString`].
139+ #[ cfg( since_api = "4.1" ) ]
140+ pub fn chars ( & self ) -> & [ char ] {
141+ // SAFETY: Godot 4.1 ensures valid UTF-32, making this safe to call.
142+ #[ allow( deprecated) ]
143+ unsafe {
144+ self . chars_unchecked ( )
145+ }
146+ }
147+
126148 ffi_methods ! {
127149 type sys:: GDExtensionStringPtr = * mut Self ;
128150
@@ -181,7 +203,17 @@ impl_builtin_traits! {
181203
182204impl fmt:: Display for GString {
183205 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
184- let s: String = self . chars_checked ( ) . iter ( ) . collect ( ) ;
206+ let s: String ;
207+
208+ #[ cfg( before_api = "4.1" ) ]
209+ {
210+ s = self . chars_checked ( ) . iter ( ) . collect ( ) ;
211+ }
212+ #[ cfg( since_api = "4.1" ) ]
213+ {
214+ s = self . chars ( ) . iter ( ) . collect ( ) ;
215+ }
216+
185217 f. write_str ( s. as_str ( ) )
186218 }
187219}
0 commit comments