Trait WinStr

Source
pub trait WinStr {
    // Required method
    fn to_bstr(&self) -> *const u16;

    // Provided method
    fn to_string(&self) -> String { ... }
}
Expand description

The WinStr trait provides methods for working with BSTRs (Binary String), a format commonly used in Windows API. BSTRs are wide strings (UTF-16) with specific memory layouts, used for interoperation with COM (Component Object Model) and other Windows-based APIs.

Required Methods§

Source

fn to_bstr(&self) -> *const u16

Converts a Rust string into a BSTR.

§Returns
  • *const u16 - A pointer to the UTF-16 encoded BSTR.

This method is implemented for &str and String, converting them into BSTRs, and for *const u16 as a passthrough.

§Example
use rustclr::WinStr;

let rust_str = "Hello, World!";
let bstr_ptr = rust_str.to_bstr();

// Use the BSTR pointer in a COM function...

Provided Methods§

Source

fn to_string(&self) -> String

Converts a BSTR (pointer *const u16) back to a Rust String.

§Returns
  • String - A String containing the text from the BSTR if the trait is implemented for *const u16. For other types, returns an empty String.
§Example
use rustclr::WinStr;

let bstr: *const u16 = /* assume a BSTR from COM */;
let rust_string = bstr.to_string();

Implementations on Foreign Types§

Source§

impl WinStr for &str

Source§

fn to_bstr(&self) -> *const u16

Converts a &str to a BSTR.

§Returns
  • *const u16 - A pointer to the UTF-16 encoded BSTR.
Source§

impl WinStr for *const u16

Source§

fn to_bstr(&self) -> *const u16

Passes through the BSTR pointer without modification.

§Returns
  • *const u16 - The original BSTR pointer.
Source§

fn to_string(&self) -> String

Converts a *const u16 BSTR to a String.

§Returns
  • String - A String containing the UTF-16 encoded text from the BSTR.
Source§

impl WinStr for String

Source§

fn to_bstr(&self) -> *const u16

Converts a String to a BSTR.

§Returns
  • *const u16 - A pointer to the UTF-16 encoded BSTR.

Implementors§