DynamicString

Struct DynamicString 

Source
pub struct DynamicString { /* private fields */ }
Expand description

A Rust version of NvdDynamicString

A DynamicString is effectively a conversion of a heap allocated string designed to work with NvDialog that works with the Rust side. In essence, it’s just another string type for Rust, but works with this crate instead. This type is useful since version v0.10 of NvDialog when the API was completely rewritten to use NvdDynamicString, a separate string type that addresses the limitations of working with raw character buffers and safety concerns.

Implementations§

Source§

impl DynamicString

Source

pub fn new<S: AsRef<str>>(data: Option<S>) -> Self

Create a new DynamicString from optional UTF-8 data.

  • If data is Some, the contents are passed to the FFI (as a C string) and mirrored into Self as well.
  • If data is None, a null is passed to nvd_string_new, which is expected (by the library) to produce an empty string.

Note that if you want to duplicate a string, you can use DynamicString::duplicate instead of allocating a brand new one.

§Safety

The returned object can be modified safely, however note that any modifications are permanent and if you received the DynamicString from another function, then trying to fetch it again will return your modified string as the internal pointer was modified.

Source

pub unsafe fn as_ptr(&self) -> *const c_char

Returns a borrowed pointer to the internal string’s buffer.

§Safety

The returned pointer is only valid as long as self is. Additionally, if the internal data was corrupted, this function may corrupt the program’s memory, therefore make sure self was not internally modified beforehand.

§Returns

A NULL-terminated, heap-allocated C-style string on success, or NULL if nvd_string_to_cstr failed.

Source

pub fn duplicate(&self) -> Self

Create a deep copy of this DynamicString.

Note that this function returns a truly unique DynamicString - It is safe to modify the returned value as self does not point to it (and vice versa).

§Safety

This function is safe to use, unless the internal structure was corrupted beforehand, in which case nvd_duplicate_string may return invalid or corrupt data. Make sure self has not been modified incorrectly before calling this function.

§Returns

A deep copy of self, assuming no errors from the FFI-side.

Source

pub fn as_str(&self) -> &str

Returns a reference to the internal String that mirrors the FFI string to avoid extra allocations.

§Safety

This function is always safe to use. If you modify self correctly, then the contents are always mirrored.

Trait Implementations§

Source§

impl Clone for DynamicString

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DynamicString

Source§

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

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

impl Display for DynamicString

Source§

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

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

impl Drop for DynamicString

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<&str> for DynamicString

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<*mut _NvdDynamicString> for DynamicString

Source§

fn from(value: *mut NvdDynamicString) -> Self

Converts to this type from the input type.
Source§

impl From<String> for DynamicString

Source§

fn from(data: String) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.