Struct Px

Source
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

Source

pub const ZERO: Self

A constant representing zero pixels.

Source

pub const MAX: Self

A constant representing the maximum possible pixel value.

Source

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);
Source

pub const fn new(value: i32) -> Self

Creates a new Px instance from an i32 value.

§Arguments
  • value - The pixel value as an i32. Negative values are allowed.
§Examples
use tessera_ui::px::Px;

let positive = Px::new(100);
let negative = Px::new(-50);
let zero = Px::new(0);
Source

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);
Source

pub fn to_dp(self) -> Dp

Converts from physical pixels to density-independent pixels (Dp).

This conversion uses the current scale factor to determine the Dp value that corresponds to this physical pixel value.

§Examples
use tessera_ui::px::Px;

let px_value = Px::new(32);
let dp_value = px_value.to_dp();
Source

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);
Source

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);
Source

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);
Source

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);
Source

pub fn from_f32(value: f32) -> Self

Creates a Px from an f32 value.

§Panics

This function may panic on overflow in debug builds when the f32 value cannot be represented as an i32.

§Examples
use tessera_ui::px::Px;

let px = Px::from_f32(42.7);
assert_eq!(px.raw(), 42);
Source

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);
Source

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);
Source

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);
Source

pub fn mul_f32(self, rhs: f32) -> Self

Multiplies the pixel value by a scalar f32.

§Arguments
  • rhs - The scalar value to multiply by
§Returns

A new Px instance with the result of the multiplication.

§Examples
use tessera_ui::px::Px;

let px = Px::new(10);
let result = px.mul_f32(2.0);
assert_eq!(result, Px::new(20));
Source

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

Source§

type Output = DimensionValue

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add for Px

Source§

type Output = Px

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign<Px> for DimensionValue

Source§

fn add_assign(&mut self, rhs: Px)

Performs the += operation. Read more
Source§

impl AddAssign for Px

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Px

Source§

fn clone(&self) -> Px

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 Debug for Px

Source§

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

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

impl Default for Px

Source§

fn default() -> Px

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

impl Div<i32> for Px

Source§

type Output = Px

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div for Px

Source§

type Output = Px

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl From<Dp> for Px

Source§

fn from(dp: Dp) -> Self

Converts to this type from the input type.
Source§

impl From<Px> for DimensionValue

Source§

fn from(value: Px) -> Self

Converts a Px value to a DimensionValue::Fixed.

Source§

impl From<Px> for Dp

Source§

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 - A Px 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
Source§

impl From<i32> for Px

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Px

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl Hash for Px

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul<i32> for Px

Source§

type Output = Px

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul for Px

Source§

type Output = Px

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Neg for Px

Source§

type Output = Px

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for Px

Source§

fn cmp(&self, other: &Px) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Px

Source§

fn eq(&self, other: &Px) -> 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 PartialOrd for Px

Source§

fn partial_cmp(&self, other: &Px) -> 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 Sub<Px> for DimensionValue

Source§

type Output = DimensionValue

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for Px

Source§

type Output = Px

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign<Px> for DimensionValue

Source§

fn sub_assign(&mut self, rhs: Px)

Performs the -= operation. Read more
Source§

impl Copy for Px

Source§

impl Eq for Px

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns a reference to the concrete type as &dyn Any.
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
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,