Skip to main content

NumericFormat

Struct NumericFormat 

Source
pub struct NumericFormat<'a, T> { /* private fields */ }
Expand description

A validated C-style format for one Dear ImGui numeric value.

The value type is part of the format type, so a format validated for f32 cannot be passed to an integer widget. Borrowed strings remain zero-copy, while owned strings remain stable after validation.

use dear_imgui_rs::{Context, NumericFormat};

let mut context = Context::create();
let ui = context.frame();
let integer_format = NumericFormat::<u32>::new("%u").unwrap();
let _ = ui
    .slider_config("value", 0.0_f32, 1.0_f32)
    .display_format(integer_format);
use dear_imgui_rs::Context;

let mut context = Context::create();
let ui = context.frame();
let _ = ui
    .slider_config("value", 0.0_f32, 1.0_f32)
    .display_format("%.2f");

Implementations§

Source§

impl<'a, T> NumericFormat<'a, T>
where T: DataTypeKind,

Source

pub fn new(storage: impl Into<Cow<'a, str>>) -> Result<Self, NumericFormatError>

Validates and retains a numeric format.

Safe formats may contain ordinary UTF-8 decoration, escaped percent signs (%%), and at most one conversion matching T. The portable ll and MSVC I64 modifiers are normalized for the compilation target. Width is limited to 31, precision to 99, and the complete UTF-8 string to 4096 bytes so downstream native parsing remains bounded.

use dear_imgui_rs::NumericFormat;

let percent = NumericFormat::<f32>::new("%.1f%%")?;
assert_eq!(percent.as_str(), "%.1f%%");
assert!(NumericFormat::<f32>::new("%s").is_err());
Source

pub unsafe fn new_unchecked(storage: impl Into<Cow<'a, str>>) -> Self

Creates a numeric format without validation.

§Safety

The format must be valid for the exact C variadic argument represented by T::KIND. It must consume at most one value argument, must not use dynamic width or precision, positional arguments, %n, or any other conversion that consumes a different argument type. Width must not exceed 31, precision must not exceed 99, flags must be valid for the conversion, and the complete string must not exceed 4096 bytes. Violating this contract can cause undefined behavior, including arbitrary memory writes.

Source

pub fn as_str(&self) -> &str

Returns the validated, target-normalized format string.

Source

pub fn borrowed(&self) -> NumericFormat<'_, T>

Borrows this format without revalidating it.

Source

pub fn into_owned(self) -> NumericFormat<'static, T>

Converts this format into an owned value that can be stored indefinitely.

Source

pub fn into_inner(self) -> Cow<'a, str>

Returns the target-normalized string storage.

Trait Implementations§

Source§

impl<T> AsRef<str> for NumericFormat<'_, T>
where T: DataTypeKind,

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a, T: Clone> Clone for NumericFormat<'a, T>

Source§

fn clone(&self) -> NumericFormat<'a, T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a, T: Debug> Debug for NumericFormat<'a, T>

Source§

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

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

impl<T> FromStr for NumericFormat<'static, T>
where T: DataTypeKind,

Source§

type Err = NumericFormatError

The associated error which can be returned from parsing.
Source§

fn from_str(value: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<'a, T: PartialEq> PartialEq for NumericFormat<'a, T>

Source§

fn eq(&self, other: &NumericFormat<'a, T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a, T: PartialEq> StructuralPartialEq for NumericFormat<'a, T>

Source§

impl<'a, T> TryFrom<&'a str> for NumericFormat<'a, T>
where T: DataTypeKind,

Source§

type Error = NumericFormatError

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

fn try_from(value: &'a str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T> TryFrom<String> for NumericFormat<'static, T>
where T: DataTypeKind,

Source§

type Error = NumericFormatError

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

fn try_from(value: String) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a, T> Freeze for NumericFormat<'a, T>

§

impl<'a, T> RefUnwindSafe for NumericFormat<'a, T>

§

impl<'a, T> Send for NumericFormat<'a, T>

§

impl<'a, T> Sync for NumericFormat<'a, T>

§

impl<'a, T> Unpin for NumericFormat<'a, T>

§

impl<'a, T> UnsafeUnpin for NumericFormat<'a, T>

§

impl<'a, T> UnwindSafe for NumericFormat<'a, T>

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, 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.