rust-pcap / pcap

Rust language pcap library
Apache License 2.0
610 stars 138 forks source link

What's the cleanest way to handle the fact that WideCString returns `FromUtf16Error` #165

Closed Wojtek242 closed 2 years ago

Wojtek242 commented 3 years ago

Currently, wstr_to_string unwraps from a string conversion which is not good. Its cstr equivalent raises an error.

However, a naive attempt to just make wstr_to_string as similar to cstr_to_string by making it:

unsafe fn wwstr_to_string(ptr: *const libc::c_char) -> Result<Option<String>, Error> {
    let string = if ptr.is_null() {
        None
    } else {
        Some(WideCString::from_ptr_str(ptr as _).to_string()?.to_owned())
    };
    Ok(string)
}

runs into the probelm that to_string above returns FromUtf16Error. Therefore, one also need to correctly convert it into the existing Error enum. There are a few options, but none of them are very exciting:

Wojtek242 commented 2 years ago

Function wstr_to_string was removed in #160.