Skip to content

Commit

Permalink
Optional dependencies in neophyte-linalg
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-harding committed Jan 16, 2025
1 parent efb6bf1 commit 1616d7d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ features = ["derive"]
[dependencies.neophyte-linalg]
path = "./neophyte-linalg"
version = "0.3.2"
features = ["all"]

[dependencies.neophyte-ui-event]
path = "./neophyte-ui-event"
Expand Down
19 changes: 13 additions & 6 deletions neophyte-linalg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ license = "MIT"
keywords = ["neovim"]
description = "Linear algebra types for Neophyte"

# TODO: Make dependencies for From impls optional

[dependencies]
wgpu-types = "24.0.0"

[dependencies.bytemuck]
version = "1.13.1"
features = ["derive"]
optional = true

[dependencies.wgpu-types]
version = "24.0.0"
optional = true

# For From impl
[dependencies.winit]
version = "0.30.0"
optional = true

[features]
bytemuck = ["dep:bytemuck"]
wgpu = ["dep:wgpu-types"]
winit = ["dep:winit"]
from-impls = ["wgpu", "winit"]
all = ["bytemuck", "from-impls"]
44 changes: 26 additions & 18 deletions neophyte-linalg/src/vec2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use bytemuck::{Pod, Zeroable};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use wgpu_types::Extent3d;
use winit::dpi::{PhysicalPosition, PhysicalSize};

/// A 2D vector type
// Align is useful to make sure padding is handled correctly in push constant
Expand All @@ -13,8 +10,10 @@ pub struct Vec2<T> {
pub y: T,
}

unsafe impl<T> Pod for Vec2<T> where T: Pod {}
unsafe impl<T> Zeroable for Vec2<T> where T: Zeroable {}
#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Pod for Vec2<T> where T: bytemuck::Pod {}
#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Zeroable for Vec2<T> where T: bytemuck::Zeroable {}

impl<T> Vec2<T>
where
Expand Down Expand Up @@ -140,31 +139,36 @@ macro_rules! float_impl {
float_impl!(f32);
float_impl!(f64);

impl<T> From<PhysicalSize<T>> for PixelVec<T> {
fn from(value: PhysicalSize<T>) -> Self {
#[cfg(feature = "winit")]
impl<T> From<winit::dpi::PhysicalSize<T>> for PixelVec<T> {
fn from(value: winit::dpi::PhysicalSize<T>) -> Self {
Self::new(value.width, value.height)
}
}

impl<T> From<PixelVec<T>> for PhysicalSize<T> {
#[cfg(feature = "winit")]
impl<T> From<PixelVec<T>> for winit::dpi::PhysicalSize<T> {
fn from(value: PixelVec<T>) -> Self {
Self::new(value.0.x, value.0.y)
}
}

impl<T> From<PhysicalPosition<T>> for PixelVec<T> {
fn from(value: PhysicalPosition<T>) -> Self {
#[cfg(feature = "winit")]
impl<T> From<winit::dpi::PhysicalPosition<T>> for PixelVec<T> {
fn from(value: winit::dpi::PhysicalPosition<T>) -> Self {
Self::new(value.x, value.y)
}
}

impl<T> From<PhysicalPosition<T>> for Vec2<T> {
fn from(value: PhysicalPosition<T>) -> Self {
#[cfg(feature = "winit")]
impl<T> From<winit::dpi::PhysicalPosition<T>> for Vec2<T> {
fn from(value: winit::dpi::PhysicalPosition<T>) -> Self {
Self::new(value.x, value.y)
}
}

impl<T> From<PixelVec<T>> for PhysicalPosition<T> {
#[cfg(feature = "winit")]
impl<T> From<PixelVec<T>> for winit::dpi::PhysicalPosition<T> {
fn from(value: PixelVec<T>) -> Self {
Self::new(value.0.x, value.0.y)
}
Expand Down Expand Up @@ -196,13 +200,15 @@ impl<T> From<[T; 2]> for Vec2<T> {
}
}

impl From<Extent3d> for PixelVec<u32> {
fn from(value: Extent3d) -> Self {
#[cfg(feature = "wgpu")]
impl From<wgpu_types::Extent3d> for PixelVec<u32> {
fn from(value: wgpu_types::Extent3d) -> Self {
Self::new(value.width, value.height)
}
}

impl From<PixelVec<u32>> for Extent3d {
#[cfg(feature = "wgpu")]
impl From<PixelVec<u32>> for wgpu_types::Extent3d {
fn from(value: PixelVec<u32>) -> Self {
Self {
width: value.0.x,
Expand Down Expand Up @@ -733,7 +739,8 @@ macro_rules! newtype_impls {

/// A [Vec2] in units of pixels
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Pod, Zeroable)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
pub struct PixelVec<T>(pub Vec2<T>);

impl<T> PixelVec<T>
Expand All @@ -747,7 +754,8 @@ where

/// A [Vec2] in units of grid cells
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Pod, Zeroable)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
pub struct CellVec<T>(pub Vec2<T>);

impl<T> CellVec<T>
Expand Down

0 comments on commit 1616d7d

Please sign in to comment.