Struct text_layout::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>
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)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
fn layout_paragraph<'a, P: ParagraphLayout<(), (), (), F>>(
paragraph: &'a str,
layout: &P,
max_width: F,
) -> Vec<&'a str> {
// Process the paragraph into its items.
let mut items = Vec::new();
for c in paragraph.chars() {
items.push(if c.is_whitespace() && items.len() != 0 {
Item::Glue {
width: F::from_num(1),
stretch: F::from_num(1),
shrink: F::from_num(0),
data: (),
}
} else {
Item::Box {
width: F::from_num(1),
data: (),
}
});
}
items.push(Item::Glue {
width: F::from_num(0),
stretch: F::MAX,
shrink: F::from_num(0),
data: (),
});
items.push(Item::Penalty {
width: F::from_num(0),
cost: F::MIN,
flagged: true,
data: (),
});
// Calculate the paragraph's breaks.
let breaks = layout.layout_paragraph(&items, max_width);
// Render the laid-out paragraph using the break positions.
let mut cursor = 0;
let mut lines = Vec::new();
let mut start = 0;
for (i, _) in paragraph.chars().enumerate() {
if i == breaks[cursor].break_at {
lines.push(¶graph[start..i]);
start = i + 1;
cursor += 1;
}
}
lines.push(¶graph[start..]);
lines
}
fn layout_text() -> Result<String, fmt::Error> {
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.";
let knuth_plass = KnuthPlass::new().with_threshold(F::MAX);
let lines = layout_paragraph(&text, &knuth_plass, F::from_num(80));
let mut result = String::new();
writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
for l in lines {
let pad = 80 - l.chars().count();
writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
}
writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
Ok(result)
}
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>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
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 moresource§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> 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<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.