1414
1515//! String wrappers for core-owned strings and strings being passed to the core
1616
17- use crate :: rc:: * ;
18- use crate :: type_archive:: TypeArchiveSnapshotId ;
19- use crate :: types:: QualifiedName ;
17+ use binaryninjacore_sys:: * ;
2018use std:: borrow:: Cow ;
2119use std:: ffi:: { c_char, CStr , CString } ;
2220use std:: fmt;
@@ -25,6 +23,10 @@ use std::mem;
2523use std:: ops:: Deref ;
2624use std:: path:: { Path , PathBuf } ;
2725
26+ use crate :: rc:: * ;
27+ use crate :: type_archive:: TypeArchiveSnapshotId ;
28+ use crate :: types:: QualifiedName ;
29+
2830// TODO: Remove or refactor this.
2931pub ( crate ) fn raw_to_string ( ptr : * const c_char ) -> Option < String > {
3032 if ptr. is_null ( ) {
@@ -66,8 +68,7 @@ pub struct BnString {
6668}
6769
6870impl BnString {
69- pub fn new < S : IntoCStr > ( s : S ) -> Self {
70- use binaryninjacore_sys:: BNAllocString ;
71+ pub fn new ( s : impl IntoCStr ) -> Self {
7172 let raw = s. to_cstr ( ) ;
7273 unsafe { Self :: from_raw ( BNAllocString ( raw. as_ptr ( ) ) ) }
7374 }
@@ -79,14 +80,13 @@ impl BnString {
7980 Self :: from_raw ( raw) . to_string_lossy ( ) . to_string ( )
8081 }
8182
82- /// Construct a BnString from an owned const char* allocated by BNAllocString
83+ /// Construct a BnString from an owned const char* allocated by [` BNAllocString`].
8384 pub ( crate ) unsafe fn from_raw ( raw : * mut c_char ) -> Self {
8485 Self { raw }
8586 }
8687
87- /// Free a raw string allocated by BNAllocString.
88+ /// Free a raw string allocated by [` BNAllocString`] .
8889 pub ( crate ) unsafe fn free_raw ( raw : * mut c_char ) {
89- use binaryninjacore_sys:: BNFreeString ;
9090 if !raw. is_null ( ) {
9191 BNFreeString ( raw) ;
9292 }
@@ -115,7 +115,6 @@ impl Drop for BnString {
115115
116116impl Clone for BnString {
117117 fn clone ( & self ) -> Self {
118- use binaryninjacore_sys:: BNAllocString ;
119118 unsafe {
120119 Self {
121120 raw : BNAllocString ( self . raw ) ,
@@ -178,7 +177,6 @@ impl CoreArrayProvider for BnString {
178177
179178unsafe impl CoreArrayProviderInner for BnString {
180179 unsafe fn free ( raw : * mut Self :: Raw , count : usize , _context : & Self :: Context ) {
181- use binaryninjacore_sys:: BNFreeStringList ;
182180 BNFreeStringList ( raw, count) ;
183181 }
184182
@@ -187,101 +185,101 @@ unsafe impl CoreArrayProviderInner for BnString {
187185 }
188186}
189187
190- pub unsafe trait IntoCStr {
188+ pub trait IntoCStr {
191189 type Result : Deref < Target = CStr > ;
192190
193191 fn to_cstr ( self ) -> Self :: Result ;
194192}
195193
196- unsafe impl IntoCStr for & CStr {
194+ impl IntoCStr for & CStr {
197195 type Result = Self ;
198196
199197 fn to_cstr ( self ) -> Self :: Result {
200198 self
201199 }
202200}
203201
204- unsafe impl IntoCStr for BnString {
202+ impl IntoCStr for BnString {
205203 type Result = Self ;
206204
207205 fn to_cstr ( self ) -> Self :: Result {
208206 self
209207 }
210208}
211209
212- unsafe impl IntoCStr for & BnString {
210+ impl IntoCStr for & BnString {
213211 type Result = BnString ;
214212
215213 fn to_cstr ( self ) -> Self :: Result {
216214 self . clone ( )
217215 }
218216}
219217
220- unsafe impl IntoCStr for CString {
218+ impl IntoCStr for CString {
221219 type Result = Self ;
222220
223221 fn to_cstr ( self ) -> Self :: Result {
224222 self
225223 }
226224}
227225
228- unsafe impl IntoCStr for & str {
226+ impl IntoCStr for & str {
229227 type Result = CString ;
230228
231229 fn to_cstr ( self ) -> Self :: Result {
232230 CString :: new ( self ) . expect ( "can't pass strings with internal nul bytes to core!" )
233231 }
234232}
235233
236- unsafe impl IntoCStr for String {
234+ impl IntoCStr for String {
237235 type Result = CString ;
238236
239237 fn to_cstr ( self ) -> Self :: Result {
240238 CString :: new ( self ) . expect ( "can't pass strings with internal nul bytes to core!" )
241239 }
242240}
243241
244- unsafe impl IntoCStr for & String {
242+ impl IntoCStr for & String {
245243 type Result = CString ;
246244
247245 fn to_cstr ( self ) -> Self :: Result {
248246 self . clone ( ) . to_cstr ( )
249247 }
250248}
251249
252- unsafe impl < ' a > IntoCStr for & ' a Cow < ' a , str > {
250+ impl < ' a > IntoCStr for & ' a Cow < ' a , str > {
253251 type Result = CString ;
254252
255253 fn to_cstr ( self ) -> Self :: Result {
256254 self . to_string ( ) . to_cstr ( )
257255 }
258256}
259257
260- unsafe impl IntoCStr for Cow < ' _ , str > {
258+ impl IntoCStr for Cow < ' _ , str > {
261259 type Result = CString ;
262260
263261 fn to_cstr ( self ) -> Self :: Result {
264262 self . to_string ( ) . to_cstr ( )
265263 }
266264}
267265
268- unsafe impl IntoCStr for & QualifiedName {
266+ impl IntoCStr for & QualifiedName {
269267 type Result = CString ;
270268
271269 fn to_cstr ( self ) -> Self :: Result {
272270 self . to_string ( ) . to_cstr ( )
273271 }
274272}
275273
276- unsafe impl IntoCStr for PathBuf {
274+ impl IntoCStr for PathBuf {
277275 type Result = CString ;
278276
279277 fn to_cstr ( self ) -> Self :: Result {
280278 self . as_path ( ) . to_cstr ( )
281279 }
282280}
283281
284- unsafe impl IntoCStr for & Path {
282+ impl IntoCStr for & Path {
285283 type Result = CString ;
286284
287285 fn to_cstr ( self ) -> Self :: Result {
@@ -290,7 +288,7 @@ unsafe impl IntoCStr for &Path {
290288 }
291289}
292290
293- unsafe impl IntoCStr for TypeArchiveSnapshotId {
291+ impl IntoCStr for TypeArchiveSnapshotId {
294292 type Result = CString ;
295293
296294 fn to_cstr ( self ) -> Self :: Result {
0 commit comments