Implement Display trait for StringRef (#36)
StringRef should implement `Display` trait to get the `to_string` method rather than implementing `to_string` by itself. Implementing `Display` is preferred and will make printing StringRef instance easier. Read more in: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string
This commit is contained in:
Родитель
acb90e9bf3
Коммит
80978b6c02
|
@ -44,10 +44,6 @@ impl StringRef {
|
|||
Self(string_ref)
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
String::from_utf8(utf8_from_cfstringref(self.0)).expect("convert bytes to a String")
|
||||
}
|
||||
|
||||
pub fn into_string(self) -> String {
|
||||
self.to_string()
|
||||
}
|
||||
|
@ -75,6 +71,14 @@ impl Drop for StringRef {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for StringRef {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let string =
|
||||
String::from_utf8(utf8_from_cfstringref(self.0)).expect("convert bytes to a String");
|
||||
write!(f, "{}", string)
|
||||
}
|
||||
}
|
||||
|
||||
fn utf8_from_cfstringref(string_ref: CFStringRef) -> Vec<u8> {
|
||||
use std::ptr;
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче