rustchromium-embedded

Transform CefStringUserfreeUtf16 into Rust String


I'm creating an AppResourceHandler and I need to obtain an URL String. Is there any easy way to obtain a String from a CefStringUserfreeUtf16 or I'm I implementing the wrong trait for this and there is an easy one.

impl ImplResourceHandler for AppResourceHandler {
    fn open(
        &self,
        request: Option<&mut impl cef::ImplRequest>,
        handle_request: Option<&mut c_int>,
        _callback: Option<&mut impl cef::ImplCallback>,
    ) -> c_int {
        // ...

        let url_cef = request.get_url();
    
    }
    
    // ...
}

Solution

  • From the docs it seems you have to go through a CefStringUtf16 and can use it's Display implementation:

    use cef::string::{CefStringUserfreeUtf16, CefStringUtf16};
    fn cef_string_userfree_utf16_to_string(
        userfree_string: &CefStringUserfreeUtf16
    ) -> String {
        let cef_string = CefStringUtf16::from(userfree_string);
        cef_string.to_string()
    }
    

    If you depend on cef_dll_sys or want to squeeze out the last bit of performance you can go through a *const _cef_string_utf16_t which saves one allocation but is lower level.