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>
impl<F: FixedSigned> Fixed<F>
pub const MAX: Self
pub const MIN: Self
Sourcepub fn from_num<Src: ToFixed>(src: Src) -> Self
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(¶graph[start..i]);
55 start = i + 1;
56 cursor += 1;
57 }
58 }
59 lines.push(¶graph[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>
impl<F: FixedSigned> Add for Fixed<F>
Source§impl<F: FixedSigned> AddAssign for Fixed<F>
impl<F: FixedSigned> AddAssign for Fixed<F>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moreSource§impl<F: FixedSigned> Debug for Fixed<F>
impl<F: FixedSigned> Debug for Fixed<F>
Source§impl<F: FixedSigned> Div for Fixed<F>
impl<F: FixedSigned> Div for Fixed<F>
Source§impl<F: FixedSigned> Mul for Fixed<F>
impl<F: FixedSigned> Mul for Fixed<F>
Source§impl<F: FixedSigned> Num for Fixed<F>
impl<F: FixedSigned> Num for Fixed<F>
Source§impl<F: FixedSigned> PartialEq for Fixed<F>
impl<F: FixedSigned> PartialEq for Fixed<F>
Source§impl<F: FixedSigned> PartialOrd for Fixed<F>
impl<F: FixedSigned> PartialOrd for Fixed<F>
Source§impl<F: FixedSigned> Sub for Fixed<F>
impl<F: FixedSigned> Sub for Fixed<F>
Source§impl<F: FixedSigned> SubAssign for Fixed<F>
impl<F: FixedSigned> SubAssign for Fixed<F>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-= operation. Read moreimpl<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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Performs the conversion.
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
Performs the conversion.
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.