Struct oaidl::DroppableBString

source ·
pub struct DroppableBString { /* private fields */ }
Expand description

Struct that holds pointer to Sys* allocated memory. It will automatically free the memory via the Sys* functions unless it has been consumed.

Safety

This wraps up a pointer to Sys* allocated memory and will automatically clean up that memory correctly unless the memory has been leaked by consume().

One would use the .consume() method when sending the pointer through FFI.

If you don’t manually free the memory yourself (correctly) or send it to an FFI function that will do so, then it will be leaked memory.

If you have a memory leak and you’re using this type, then check your use of consume.

Example

extern crate oaidl;
extern crate widestring;
 
use oaidl::{BStringError, BStringExt, DroppableBString};
use widestring::U16String;
 
fn main() -> Result<(), BStringError> {
    let s = U16String::from_str("The first step to doing anything is to believe you can do it. See it finished in your mind before you ever start. It takes dark in order to show light.");
    let dbs = s.consume_to_managed_bstr()?;
    drop(dbs); // Correctly deallocates allocated memory.
    Ok(())
}

Implementations§

source§

impl DroppableBString

source

pub unsafe fn consume(&mut self) -> *mut u16

consume() -> *mut u16 returns the contained data while also setting a flag that the data has been consumed. It is your responsibility to manage the memory yourself. Most uses of BSTR in FFI will free the memory for you.

This method is very unsafe to use unless you know how to handle it correctly, hence the unsafe marker.

Trait Implementations§

source§

impl Debug for DroppableBString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for DroppableBString

source§

fn drop(&mut self)

Handles freeing the allocated BSTR correctly via SysFreeString. The only (safe) way to construct a DroppableBString is via an [allocate_managed_bstr] call.

source§

impl Hash for DroppableBString

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<DroppableBString> for DroppableBString

source§

fn eq(&self, other: &DroppableBString) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<DroppableBString> for DroppableBString

source§

fn partial_cmp(&self, other: &DroppableBString) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for DroppableBString

source§

impl StructuralEq for DroppableBString

source§

impl StructuralPartialEq for DroppableBString

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, F> TryConvert<T, F> for Twhere T: From<T>, F: Fail,

source§

fn try_convert(val: T) -> Result<T, F>

Blanket TryConvert implementation wherever a From is implemented for T. (Which is all types.) This avoids repetitive code. The compiler monomorphizes the code for F. And because its always an Ok, should optimize this code away.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.