Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/kas-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ ab_glyph = { version = "0.2.10", optional = true }
swash = { version = "0.2.4", features = ["scale"] }
linearize = { version = "0.1.5", features = ["derive"] }
kas-text = "0.9.0"
easy-cast = "0.6.0"
easy-cast = "0.6.1"
pulldown-cmark = { version = "0.13.0", optional = true }

[dependencies.kas-macros]
Expand Down
51 changes: 31 additions & 20 deletions crates/kas-core/src/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,6 @@ macro_rules! impl_common {
self.0 > rhs.0 && self.1 > rhs.1
}
}

impl From<(i32, i32)> for $T {
#[inline]
fn from(v: (i32, i32)) -> Self {
Self(v.0, v.1)
}
}
impl Conv<(i32, i32)> for $T {
#[inline]
fn conv(v: (i32, i32)) -> Self {
Self(v.0, v.1)
}
#[inline]
fn try_conv(v: (i32, i32)) -> Result<Self> {
Ok(Self::conv(v))
}
}
};
}

Expand Down Expand Up @@ -198,6 +181,13 @@ impl Coord {
}
}

impl From<(i32, i32)> for Coord {
#[inline]
fn from(v: (i32, i32)) -> Self {
Self(v.0, v.1)
}
}

impl std::ops::Sub for Coord {
type Output = Offset;

Expand Down Expand Up @@ -336,6 +326,21 @@ impl Size {
}
}

impl Conv<(i32, i32)> for Size {
fn try_conv(v: (i32, i32)) -> Result<Self> {
if v.0 >= 0 && v.1 >= 0 {
Ok(Size(v.0, v.1))
} else {
Err(Error::Range)
}
}

#[inline]
fn conv(v: (i32, i32)) -> Self {
Self::new(v.0, v.1)
}
}

impl std::ops::Add for Size {
type Output = Self;

Expand Down Expand Up @@ -400,12 +405,9 @@ impl std::ops::Div<i32> for Size {
}

/// Convert an [`Offset`] into a [`Coord`]
///
/// In debug mode this asserts that the result is non-negative.
impl Conv<Offset> for Coord {
#[inline]
fn try_conv(v: Offset) -> Result<Self> {
debug_assert!(v.0 >= 0 && v.1 >= 0, "Coord::conv({v:?}): negative value");
Ok(Self(v.0, v.1))
}
}
Expand Down Expand Up @@ -488,6 +490,13 @@ impl Offset {
}
}

impl From<(i32, i32)> for Offset {
#[inline]
fn from(v: (i32, i32)) -> Self {
Self(v.0, v.1)
}
}

impl std::ops::Neg for Offset {
type Output = Self;

Expand Down Expand Up @@ -567,6 +576,8 @@ impl Conv<Offset> for kas_text::Vec2 {
}
}

impl_via_from!((i32, i32): Coord, Offset);

/// An axis-aligned rectangular region
///
/// The region is defined by a point `pos` and an extent `size`, allowing easy
Expand Down
51 changes: 4 additions & 47 deletions crates/kas-core/src/geom/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,33 +594,15 @@ macro_rules! impl_vec2 {
$T(arg.0, arg.1)
}
}
impl Conv<($f, $f)> for $T {
#[inline]
fn conv(arg: ($f, $f)) -> Self {
$T(arg.0, arg.1)
}
#[inline]
fn try_conv(v: ($f, $f)) -> Result<Self> {
Ok(Self::conv(v))
}
}
impl_via_from!(($f, $f): $T);

impl From<$T> for ($f, $f) {
#[inline]
fn from(v: $T) -> Self {
(v.0, v.1)
}
}
impl Conv<$T> for ($f, $f) {
#[inline]
fn conv(v: $T) -> Self {
(v.0, v.1)
}
#[inline]
fn try_conv(v: $T) -> Result<Self> {
Ok(Self::conv(v))
}
}
impl_via_from!($T: ($f, $f));

impl From<winit::dpi::PhysicalPosition<$f>> for $T {
#[inline]
Expand All @@ -644,46 +626,21 @@ impl From<kas_text::Vec2> for Vec2 {
Vec2(size.0, size.1)
}
}
impl Conv<kas_text::Vec2> for Vec2 {
#[inline]
fn conv(size: kas_text::Vec2) -> Self {
Vec2(size.0, size.1)
}
#[inline]
fn try_conv(v: kas_text::Vec2) -> Result<Self> {
Ok(Self::conv(v))
}
}
impl_via_from!(kas_text::Vec2: Vec2);

impl From<Vec2> for kas_text::Vec2 {
#[inline]
fn from(size: Vec2) -> kas_text::Vec2 {
kas_text::Vec2(size.0, size.1)
}
}
impl Conv<Vec2> for kas_text::Vec2 {
#[inline]
fn conv(size: Vec2) -> kas_text::Vec2 {
kas_text::Vec2(size.0, size.1)
}
#[inline]
fn try_conv(v: Vec2) -> Result<Self> {
Ok(Self::conv(v))
}
}

impl From<Vec2> for DVec2 {
#[inline]
fn from(v: Vec2) -> DVec2 {
DVec2(v.0.into(), v.1.into())
}
}
impl Conv<Vec2> for DVec2 {
#[inline]
fn try_conv(v: Vec2) -> Result<DVec2> {
Ok(DVec2(v.0.into(), v.1.into()))
}
}
impl_via_from!(Vec2: kas_text::Vec2, DVec2);
impl ConvApprox<DVec2> for Vec2 {
fn try_conv_approx(size: DVec2) -> Result<Vec2> {
Ok(Vec2(size.0.try_cast_approx()?, size.1.try_cast_approx()?))
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-soft/src/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl<F: Format> Allocator for Atlases<F> {

let origin = (alloc.rectangle.min.x.cast(), alloc.rectangle.min.y.cast());

let tex_size = Vec2::conv(Size::from(tex_size));
let tex_size = Vec2::conv(Size::conv(tex_size));
let a = to_vec2(alloc.rectangle.min);
let b = to_vec2(alloc.rectangle.max);
debug_assert!(Vec2::ZERO <= a && a <= b && b <= tex_size);
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-wgpu/src/draw/atlases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl<I: bytemuck::Pod> Allocator for Pipeline<I> {

let origin = (alloc.rectangle.min.x.cast(), alloc.rectangle.min.y.cast());

let tex_size = Vec2::conv(Size::from(tex_size));
let tex_size = Vec2::conv(Size::conv(tex_size));
let a = to_vec2(alloc.rectangle.min) / tex_size;
let b = to_vec2(alloc.rectangle.max) / tex_size;
debug_assert!(Vec2::ZERO <= a && a <= b && b <= Vec2::splat(1.0));
Expand Down