Fixed

Struct Fixed 

Source
pub struct Fixed<F: FixedSigned>(/* private fields */);
Expand description

Wraps a signed fixed-point number. All operations are saturating so that the underlying representation’s minimum and maximum values are able to stand in for -∞ and +∞.

Implementations§

Source§

impl<F: FixedSigned> Fixed<F>

Source

pub const MAX: Self

Source

pub const MIN: Self

Source

pub fn from_num<Src: ToFixed>(src: Src) -> Self

Examples found in repository?
examples/fixed.rs (line 20)
10fn layout_paragraph<'a, P: ParagraphLayout<(), (), (), F>>(
11    paragraph: &'a str,
12    layout: &P,
13    max_width: F,
14) -> Vec<&'a str> {
15    // Process the paragraph into its items.
16    let mut items = Vec::new();
17    for c in paragraph.chars() {
18        items.push(if c.is_whitespace() && items.len() != 0 {
19            Item::Glue {
20                width: F::from_num(1),
21                stretch: F::from_num(1),
22                shrink: F::from_num(0),
23                data: (),
24            }
25        } else {
26            Item::Box {
27                width: F::from_num(1),
28                data: (),
29            }
30        });
31    }
32    items.push(Item::Glue {
33        width: F::from_num(0),
34        stretch: F::MAX,
35        shrink: F::from_num(0),
36        data: (),
37    });
38    items.push(Item::Penalty {
39        width: F::from_num(0),
40        cost: F::MIN,
41        flagged: true,
42        data: (),
43    });
44
45    // Calculate the paragraph's breaks.
46    let breaks = layout.layout_paragraph(&items, max_width);
47
48    // Render the laid-out paragraph using the break positions.
49    let mut cursor = 0;
50    let mut lines = Vec::new();
51    let mut start = 0;
52    for (i, _) in paragraph.chars().enumerate() {
53        if i == breaks[cursor].break_at {
54            lines.push(&paragraph[start..i]);
55            start = i + 1;
56            cursor += 1;
57        }
58    }
59    lines.push(&paragraph[start..]);
60    lines
61}
62
63fn layout_text() -> Result<String, fmt::Error> {
64    let text = "  Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.";
65    let knuth_plass = KnuthPlass::new().with_threshold(F::MAX);
66    let lines = layout_paragraph(&text, &knuth_plass, F::from_num(80));
67    let mut result = String::new();
68    writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
69    for l in lines {
70        let pad = 80 - l.chars().count();
71        writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
72    }
73    writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
74    Ok(result)
75}

Trait Implementations§

Source§

impl<F: FixedSigned> Add for Fixed<F>

Source§

type Output = Fixed<F>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl<F: FixedSigned> AddAssign for Fixed<F>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<F: Clone + FixedSigned> Clone for Fixed<F>

Source§

fn clone(&self) -> Fixed<F>

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<F: FixedSigned> Debug for Fixed<F>

Source§

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

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

impl<F: Default + FixedSigned> Default for Fixed<F>

Source§

fn default() -> Fixed<F>

Returns the “default value” for a type. Read more
Source§

impl<F: FixedSigned> Div for Fixed<F>

Source§

type Output = Fixed<F>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self

Performs the / operation. Read more
Source§

impl<F: FixedSigned> Mul for Fixed<F>

Source§

type Output = Fixed<F>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl<F: FixedSigned> Num for Fixed<F>

Source§

const INFINITY: Self = Self::MAX

Source§

const NEG_INFINITY: Self = Self::MIN

Source§

fn from(i: i16) -> Self

Source§

fn abs(self) -> Self

Source§

fn powi(self, y: u32) -> Self

Source§

fn rat(num: i16, denom: i16) -> Self

Source§

impl<F: FixedSigned> PartialEq for Fixed<F>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<F: FixedSigned> PartialOrd for Fixed<F>

Source§

fn partial_cmp(&self, other: &Self) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<F: FixedSigned> Sub for Fixed<F>

Source§

type Output = Fixed<F>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl<F: FixedSigned> SubAssign for Fixed<F>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<F: Copy + FixedSigned> Copy for Fixed<F>

Auto Trait Implementations§

§

impl<F> Freeze for Fixed<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for Fixed<F>
where F: RefUnwindSafe,

§

impl<F> Send for Fixed<F>
where F: Send,

§

impl<F> Sync for Fixed<F>
where F: Sync,

§

impl<F> Unpin for Fixed<F>
where F: Unpin,

§

impl<F> UnwindSafe for Fixed<F>
where F: UnwindSafe,

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.