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
use crate::binds::MonoString;
use crate::domain::Domain;
use crate::gc::{gc_unsafe_enter, gc_unsafe_exit, GCHandle};
use crate::interop::{InteropClass, InteropRecive, InteropSend};
///needed for docs
#[allow(unused_imports)]
use crate::object::Object;
use crate::Class;
use crate::ObjectTrait;
use core::ptr::null_mut;
use std::ffi::CString;
#[warn(unused_imports)]
///Representaiton of [`Object`] of type **System.String**.
pub struct MString {
    #[cfg(not(feature = "referneced_objects"))]
    s_ptr: *mut MonoString,
    #[cfg(feature = "referneced_objects")]
    handle: GCHandle,
}
impl MString {
    ///Creates new managed **String** in *domain* with content of *string*.
    #[must_use]
    pub fn new(domain: &Domain, string: &str) -> Self {
        let cstr = CString::new(string).expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let res = unsafe {
            Self::from_ptr(crate::binds::mono_string_new(domain.get_ptr(), cstr.as_ptr()).cast())
        }
        .expect(crate::STR2CSTR_ERR);
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        drop(cstr);
        res
    }
    ///Compares two managed strings. Returns true if their **content** is equal, not if they are the same **object**.
    #[must_use]
    pub fn is_equal(&self, other: &Self) -> bool {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let equ = unsafe {
            crate::binds::mono_string_equal(self.get_ptr().cast(), other.get_ptr().cast()) != 0
        };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        equ
    }
    ///Creates hash of a [`String`].
    #[must_use]
    pub fn hash(&self) -> u32 {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let hsh = unsafe { crate::binds::mono_string_hash(self.get_ptr().cast()) };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        hsh
    }
}
impl InteropClass for MString {
    fn get_mono_class() -> Class {
        Class::get_string()
    }
}
impl ToString for MString {
    ///Converts [`MString`] to [`String`]  
    fn to_string(&self) -> String {
        #[cfg(feature = "referneced_objects")]
        let marker = gc_unsafe_enter();
        let cstr = unsafe {
            CString::from_raw(crate::binds::mono_string_to_utf8(
                self.get_ptr().cast::<MonoString>(),
            ))
        };
        let res = cstr.to_str().expect("Colud not create String!").to_owned();
        unsafe { crate::binds::mono_free(cstr.into_raw().cast::<std::os::raw::c_void>()) };
        #[cfg(feature = "referneced_objects")]
        gc_unsafe_exit(marker);
        res
    }
}
use crate::binds::MonoObject;
use crate::Exception;
impl ObjectTrait for MString {
    #[must_use]
    fn get_ptr(&self) -> *mut MonoObject {
        #[cfg(not(feature = "referneced_objects"))]
        {
            self.s_ptr.cast()
        }
        #[cfg(feature = "referneced_objects")]
        {
            self.handle.get_target()
        }
    }
    /// Creates [`MString`] form pointer , or returns [`None`] if pointer equal to null.
    /// # Safety
    /// *ptr* must be either a valid [`MonoString`] pointer or null. Pasing any other value will lead to undefined behaviour.
    unsafe fn from_ptr_unchecked(ptr: *mut MonoObject) -> Self {
        #[cfg(not(feature = "referneced_objects"))]
        {
            Self { s_ptr: ptr.cast() }
        }
        #[cfg(feature = "referneced_objects")]
        {
            Self {
                handle: GCHandle::create_default(ptr.cast::<MonoObject>()),
            }
        }
    }
    fn to_mstring(&self) -> Result<Option<MString>, Exception> {
        Ok(Some(self.clone()))
    }
}
impl Clone for MString {
    fn clone(&self) -> Self {
        unsafe { Self::from_ptr(self.get_ptr()).unwrap() } //If object exists then it can't be null
    }
}
impl<O: ObjectTrait> PartialEq<O> for MString {
    fn eq(&self, other: &O) -> bool {
        self.get_ptr().cast() == other.get_ptr()
    }
}