Skip to content

Commit b8c027e

Browse files
madsmtmOsspial
andcommitted
Deduplicate [Logical|Physical][Size|Position] From impls using a macro
Co-authored-by: Osspial <[email protected]>
1 parent 2969acd commit b8c027e

1 file changed

Lines changed: 46 additions & 148 deletions

File tree

dpi/src/lib.rs

Lines changed: 46 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,48 @@ impl<P: Pixel> From<LogicalUnit<P>> for PixelUnit {
360360
}
361361
}
362362

363+
macro_rules! vec2_from_impls {
364+
($t:ident, $a:ident, $b:ident, $mint_ty:ident) => {
365+
impl<P: Pixel, X: Pixel> From<(X, X)> for $t<P> {
366+
fn from(($a, $b): (X, X)) -> Self {
367+
Self::new($a.cast(), $b.cast())
368+
}
369+
}
370+
371+
impl<P: Pixel, X: Pixel> From<$t<P>> for (X, X) {
372+
fn from(p: $t<P>) -> Self {
373+
(p.$a.cast(), p.$b.cast())
374+
}
375+
}
376+
377+
impl<P: Pixel, X: Pixel> From<[X; 2]> for $t<P> {
378+
fn from([$a, $b]: [X; 2]) -> Self {
379+
Self::new($a.cast(), $b.cast())
380+
}
381+
}
382+
383+
impl<P: Pixel, X: Pixel> From<$t<P>> for [X; 2] {
384+
fn from(p: $t<P>) -> Self {
385+
[p.$a.cast(), p.$b.cast()]
386+
}
387+
}
388+
389+
#[cfg(feature = "mint")]
390+
impl<P: Pixel> From<mint::$mint_ty<P>> for $t<P> {
391+
fn from(p: mint::$mint_ty<P>) -> Self {
392+
Self::new(p.x, p.y)
393+
}
394+
}
395+
396+
#[cfg(feature = "mint")]
397+
impl<P: Pixel> From<$t<P>> for mint::$mint_ty<P> {
398+
fn from(p: $t<P>) -> Self {
399+
Self { x: p.$a, y: p.$b }
400+
}
401+
}
402+
};
403+
}
404+
363405
/// A position represented in logical pixels.
364406
///
365407
/// The position is stored as floats, so please be careful. Casting floats to integers truncates the
@@ -402,43 +444,7 @@ impl<P: Pixel> LogicalPosition<P> {
402444
}
403445
}
404446

405-
impl<P: Pixel, X: Pixel> From<(X, X)> for LogicalPosition<P> {
406-
fn from((x, y): (X, X)) -> LogicalPosition<P> {
407-
LogicalPosition::new(x.cast(), y.cast())
408-
}
409-
}
410-
411-
impl<P: Pixel, X: Pixel> From<LogicalPosition<P>> for (X, X) {
412-
fn from(p: LogicalPosition<P>) -> (X, X) {
413-
(p.x.cast(), p.y.cast())
414-
}
415-
}
416-
417-
impl<P: Pixel, X: Pixel> From<[X; 2]> for LogicalPosition<P> {
418-
fn from([x, y]: [X; 2]) -> LogicalPosition<P> {
419-
LogicalPosition::new(x.cast(), y.cast())
420-
}
421-
}
422-
423-
impl<P: Pixel, X: Pixel> From<LogicalPosition<P>> for [X; 2] {
424-
fn from(p: LogicalPosition<P>) -> [X; 2] {
425-
[p.x.cast(), p.y.cast()]
426-
}
427-
}
428-
429-
#[cfg(feature = "mint")]
430-
impl<P: Pixel> From<mint::Point2<P>> for LogicalPosition<P> {
431-
fn from(p: mint::Point2<P>) -> Self {
432-
Self::new(p.x, p.y)
433-
}
434-
}
435-
436-
#[cfg(feature = "mint")]
437-
impl<P: Pixel> From<LogicalPosition<P>> for mint::Point2<P> {
438-
fn from(p: LogicalPosition<P>) -> Self {
439-
mint::Point2 { x: p.x, y: p.y }
440-
}
441-
}
447+
vec2_from_impls!(LogicalPosition, x, y, Point2);
442448

443449
/// A position represented in physical pixels.
444450
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
@@ -478,43 +484,7 @@ impl<P: Pixel> PhysicalPosition<P> {
478484
}
479485
}
480486

481-
impl<P: Pixel, X: Pixel> From<(X, X)> for PhysicalPosition<P> {
482-
fn from((x, y): (X, X)) -> PhysicalPosition<P> {
483-
PhysicalPosition::new(x.cast(), y.cast())
484-
}
485-
}
486-
487-
impl<P: Pixel, X: Pixel> From<PhysicalPosition<P>> for (X, X) {
488-
fn from(p: PhysicalPosition<P>) -> (X, X) {
489-
(p.x.cast(), p.y.cast())
490-
}
491-
}
492-
493-
impl<P: Pixel, X: Pixel> From<[X; 2]> for PhysicalPosition<P> {
494-
fn from([x, y]: [X; 2]) -> PhysicalPosition<P> {
495-
PhysicalPosition::new(x.cast(), y.cast())
496-
}
497-
}
498-
499-
impl<P: Pixel, X: Pixel> From<PhysicalPosition<P>> for [X; 2] {
500-
fn from(p: PhysicalPosition<P>) -> [X; 2] {
501-
[p.x.cast(), p.y.cast()]
502-
}
503-
}
504-
505-
#[cfg(feature = "mint")]
506-
impl<P: Pixel> From<mint::Point2<P>> for PhysicalPosition<P> {
507-
fn from(p: mint::Point2<P>) -> Self {
508-
Self::new(p.x, p.y)
509-
}
510-
}
511-
512-
#[cfg(feature = "mint")]
513-
impl<P: Pixel> From<PhysicalPosition<P>> for mint::Point2<P> {
514-
fn from(p: PhysicalPosition<P>) -> Self {
515-
mint::Point2 { x: p.x, y: p.y }
516-
}
517-
}
487+
vec2_from_impls!(PhysicalPosition, x, y, Point2);
518488

519489
/// A size represented in logical pixels.
520490
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
@@ -554,43 +524,7 @@ impl<P: Pixel> LogicalSize<P> {
554524
}
555525
}
556526

557-
impl<P: Pixel, X: Pixel> From<(X, X)> for LogicalSize<P> {
558-
fn from((x, y): (X, X)) -> LogicalSize<P> {
559-
LogicalSize::new(x.cast(), y.cast())
560-
}
561-
}
562-
563-
impl<P: Pixel, X: Pixel> From<LogicalSize<P>> for (X, X) {
564-
fn from(s: LogicalSize<P>) -> (X, X) {
565-
(s.width.cast(), s.height.cast())
566-
}
567-
}
568-
569-
impl<P: Pixel, X: Pixel> From<[X; 2]> for LogicalSize<P> {
570-
fn from([x, y]: [X; 2]) -> LogicalSize<P> {
571-
LogicalSize::new(x.cast(), y.cast())
572-
}
573-
}
574-
575-
impl<P: Pixel, X: Pixel> From<LogicalSize<P>> for [X; 2] {
576-
fn from(s: LogicalSize<P>) -> [X; 2] {
577-
[s.width.cast(), s.height.cast()]
578-
}
579-
}
580-
581-
#[cfg(feature = "mint")]
582-
impl<P: Pixel> From<mint::Vector2<P>> for LogicalSize<P> {
583-
fn from(v: mint::Vector2<P>) -> Self {
584-
Self::new(v.x, v.y)
585-
}
586-
}
587-
588-
#[cfg(feature = "mint")]
589-
impl<P: Pixel> From<LogicalSize<P>> for mint::Vector2<P> {
590-
fn from(s: LogicalSize<P>) -> Self {
591-
mint::Vector2 { x: s.width, y: s.height }
592-
}
593-
}
527+
vec2_from_impls!(LogicalSize, width, height, Vector2);
594528

595529
/// A size represented in physical pixels.
596530
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
@@ -627,43 +561,7 @@ impl<P: Pixel> PhysicalSize<P> {
627561
}
628562
}
629563

630-
impl<P: Pixel, X: Pixel> From<(X, X)> for PhysicalSize<P> {
631-
fn from((x, y): (X, X)) -> PhysicalSize<P> {
632-
PhysicalSize::new(x.cast(), y.cast())
633-
}
634-
}
635-
636-
impl<P: Pixel, X: Pixel> From<PhysicalSize<P>> for (X, X) {
637-
fn from(s: PhysicalSize<P>) -> (X, X) {
638-
(s.width.cast(), s.height.cast())
639-
}
640-
}
641-
642-
impl<P: Pixel, X: Pixel> From<[X; 2]> for PhysicalSize<P> {
643-
fn from([x, y]: [X; 2]) -> PhysicalSize<P> {
644-
PhysicalSize::new(x.cast(), y.cast())
645-
}
646-
}
647-
648-
impl<P: Pixel, X: Pixel> From<PhysicalSize<P>> for [X; 2] {
649-
fn from(s: PhysicalSize<P>) -> [X; 2] {
650-
[s.width.cast(), s.height.cast()]
651-
}
652-
}
653-
654-
#[cfg(feature = "mint")]
655-
impl<P: Pixel> From<mint::Vector2<P>> for PhysicalSize<P> {
656-
fn from(v: mint::Vector2<P>) -> Self {
657-
Self::new(v.x, v.y)
658-
}
659-
}
660-
661-
#[cfg(feature = "mint")]
662-
impl<P: Pixel> From<PhysicalSize<P>> for mint::Vector2<P> {
663-
fn from(s: PhysicalSize<P>) -> Self {
664-
mint::Vector2 { x: s.width, y: s.height }
665-
}
666-
}
564+
vec2_from_impls!(PhysicalSize, width, height, Vector2);
667565

668566
/// A size that's either physical or logical.
669567
#[derive(Debug, Copy, Clone, PartialEq)]

0 commit comments

Comments
 (0)