pub struct Px(pub i32);
Expand description
A physical pixel coordinate value.
This type represents a single coordinate value in physical pixel space. Physical pixels
correspond directly to screen pixels and are used internally by the rendering system.
Unlike density-independent pixels (Dp
), physical pixels are not scaled based on
screen density.
§Features
- Supports negative values for scrolling and off-screen positioning
- Provides arithmetic operations (addition, subtraction, multiplication, division)
- Includes saturating arithmetic to prevent overflow
- Converts to/from density-independent pixels (
Dp
) - Converts to/from floating-point values with overflow protection
§Examples
use tessera_ui::px::Px;
// Create pixel values
let px1 = Px::new(100);
let px2 = Px::new(-50); // Negative values supported
// Arithmetic operations
let sum = px1 + px2; // Px(50)
let doubled = px1 * 2; // Px(200)
// Saturating arithmetic prevents overflow
let max_px = Px::new(i32::MAX);
let safe_add = max_px.saturating_add(Px::new(1)); // Still Px(i32::MAX)
// Convert to absolute value for rendering
let abs_value = Px::new(-10).abs(); // 0 (negative becomes 0)
Tuple Fields§
§0: i32
Implementations§
Source§impl Px
impl Px
Sourcepub fn raw(self) -> i32
pub fn raw(self) -> i32
Returns the raw i32 value.
This provides direct access to the underlying integer value.
§Examples
use tessera_ui::px::Px;
let px = Px::new(42);
assert_eq!(px.raw(), 42);
Sourcepub fn from_dp(dp: Dp) -> Self
pub fn from_dp(dp: Dp) -> Self
Converts from density-independent pixels (Dp
) to physical pixels.
This conversion uses the current scale factor to determine how many physical pixels correspond to the given Dp value. The scale factor is typically determined by the screen’s pixel density.
§Arguments
dp
- The density-independent pixel value to convert
§Examples
use tessera_ui::px::Px;
use tessera_ui::dp::Dp;
let dp_value = Dp::new(16.0);
let px_value = Px::from_dp(dp_value);
Sourcepub fn abs(self) -> u32
pub fn abs(self) -> u32
Returns the absolute value as a u32
This method is primarily used for coordinate conversion during rendering, where negative coordinates need to be handled appropriately.
§Examples
use tessera_ui::px::Px;
assert_eq!(Px::new(10).abs(), 10);
assert_eq!(Px::new(-5).abs(), 5);
assert_eq!(Px::new(0).abs(), 0);
Sourcepub fn positive(self) -> u32
pub fn positive(self) -> u32
Returns only the positive value, or zero if negative.
This is useful for ensuring that pixel values are always non-negative, especially when dealing with rendering or layout calculations.
§Examples
use tessera_ui::px::Px;
assert_eq!(Px::new(10).positive(), 10);
assert_eq!(Px::new(-5).positive(), 0);
assert_eq!(Px::new(0).positive(), 0);
Sourcepub fn negative(self) -> i32
pub fn negative(self) -> i32
Returns the negative value, or zero if positive.
This is useful for ensuring that pixel values are always non-positive, especially when dealing with rendering or layout calculations.
§Examples
use tessera_ui::px::Px;
assert_eq!(Px::new(10).negative(), 0);
assert_eq!(Px::new(-5).negative(), -5);
assert_eq!(Px::new(0).negative(), 0);
Sourcepub fn to_f32(self) -> f32
pub fn to_f32(self) -> f32
Converts the pixel value to f32.
§Examples
use tessera_ui::px::Px;
let px = Px::new(42);
assert_eq!(px.to_f32(), 42.0);
Sourcepub fn saturating_from_f32(value: f32) -> Self
pub fn saturating_from_f32(value: f32) -> Self
Creates a Px
from an f32 value, saturating at the numeric bounds instead of overflowing.
This is the safe alternative to from_f32
that handles overflow
by clamping the value to the valid i32 range.
§Examples
use tessera_ui::px::Px;
let normal = Px::saturating_from_f32(42.7);
assert_eq!(normal.raw(), 42);
let max_val = Px::saturating_from_f32(f32::MAX);
assert_eq!(max_val.raw(), i32::MAX);
let min_val = Px::saturating_from_f32(f32::MIN);
assert_eq!(min_val.raw(), i32::MIN);
Sourcepub fn saturating_add(self, rhs: Self) -> Self
pub fn saturating_add(self, rhs: Self) -> Self
Saturating integer addition.
Computes self + rhs
, saturating at the numeric bounds instead of overflowing.
This prevents integer overflow by clamping the result to the valid i32 range.
§Arguments
rhs
- The right-hand side value to add
§Examples
use tessera_ui::px::Px;
let a = Px::new(10);
let b = Px::new(5);
assert_eq!(a.saturating_add(b), Px::new(15));
// Prevents overflow
let max = Px::new(i32::MAX);
assert_eq!(max.saturating_add(Px::new(1)), max);
Sourcepub fn saturating_sub(self, rhs: Self) -> Self
pub fn saturating_sub(self, rhs: Self) -> Self
Saturating integer subtraction.
Computes self - rhs
, saturating at the numeric bounds instead of overflowing.
This prevents integer underflow by clamping the result to the valid i32 range.
§Arguments
rhs
- The right-hand side value to subtract
§Examples
use tessera_ui::px::Px;
let a = Px::new(10);
let b = Px::new(5);
assert_eq!(a.saturating_sub(b), Px::new(5));
// Prevents underflow
let min = Px::new(i32::MIN);
assert_eq!(min.saturating_sub(Px::new(1)), min);
Sourcepub fn div_f32(self, rhs: f32) -> Self
pub fn div_f32(self, rhs: f32) -> Self
Divides the pixel value by a scalar f32.
§Arguments
rhs
- The scalar value to divide by
§Returns
A new Px
instance with the result of the division.
§Panics
This function may panic if rhs
is zero, as division by zero is undefined.
§Examples
use tessera_ui::px::Px;
let px = Px::new(20);
let result = px.div_f32(2.0);
assert_eq!(result, Px::new(10));
Trait Implementations§
Source§impl Add<Px> for DimensionValue
impl Add<Px> for DimensionValue
Source§impl AddAssign<Px> for DimensionValue
impl AddAssign<Px> for DimensionValue
Source§fn add_assign(&mut self, rhs: Px)
fn add_assign(&mut self, rhs: Px)
+=
operation. Read moreSource§impl AddAssign for Px
impl AddAssign for Px
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moreSource§impl From<Px> for DimensionValue
impl From<Px> for DimensionValue
Source§impl From<Px> for Dp
impl From<Px> for Dp
Source§fn from(px: Px) -> Self
fn from(px: Px) -> Self
Creates a Dp
instance from a Px
(physical pixels) value.
This implementation enables seamless conversion between the two pixel types used in the Tessera framework. The conversion applies the inverse of the current scale factor to convert physical pixels back to density-independent pixels.
§Arguments
px
- APx
instance representing physical pixels
§Examples
use tessera_ui::{Dp, Px};
let px_value = Px::from_f32(48.0);
let dp_value: Dp = px_value.into();
// Or using From::from
let dp_value2 = Dp::from(px_value);
§See Also
to_px
- For the inverse conversionfrom_pixels_f64
- For direct pixel-to-dp conversion
Source§impl Ord for Px
impl Ord for Px
Source§impl PartialOrd for Px
impl PartialOrd for Px
Source§impl Sub<Px> for DimensionValue
impl Sub<Px> for DimensionValue
Source§impl SubAssign<Px> for DimensionValue
impl SubAssign<Px> for DimensionValue
Source§fn sub_assign(&mut self, rhs: Px)
fn sub_assign(&mut self, rhs: Px)
-=
operation. Read moreimpl Copy for Px
impl Eq for Px
impl StructuralPartialEq for Px
Auto Trait Implementations§
impl Freeze for Px
impl RefUnwindSafe for Px
impl Send for Px
impl Sync for Px
impl Unpin for Px
impl UnwindSafe for Px
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.