代码内容#[cfg(not(any(feature scalar-math, target_arch spirv)))]#[repr(C)]pubstructVec3T{pubx:T,puby:T,pubz:T,}#[cfg(not(any(feature scalar-math, target_arch spirv)))]#[repr(C)]pubstructVec4T{pubx:T,puby:T,pubz:T,pubw:T,}#[cfg(not(any(feature scalar-math, target_arch spirv)))]#[repr(C)]pubstructCols2V{pubx_axis:V,puby_axis:V,}#[repr(C)]pubstructCols3V{pubx_axis:V,puby_axis:V,pubz_axis:V,}#[repr(C)]pubstructCols4V{pubx_axis:V,puby_axis:V,pubz_axis:V,pubw_axis:V,}~~~ ## 详细解释 ###1.条件编译属性 ~~~rust#[cfg(not(any(feature scalar-math, target_arch spirv)))]~~~-not(any(...))两个条件都不满足时才编译-featurescalar-math启用了scalar-math特性-target_archspirv目标架构是SPIR-VGPU着色器-只有Vec3、Vec4和Cols2有条件编译Cols3和Cols4始终可用 ###2.向量类型 ~~~rustpubstructVec3T{x:T,y:T,z:T}pubstructVec4T{x:T,y:T,z:T,w:T}~~~-#[repr(C)]保证C语言兼容的内存布局-泛型T支持不同精度类型如f32、f64-分别表示3D和4D向量的坐标分量 ###3.矩阵类型列优先存储 ~~~rustpubstructCols2V{x_axis:V,y_axis:V}// 2列矩阵pubstructCols3V{x_axis:V,y_axis:V,z_axis:V}// 3列矩阵pubstructCols4V{x_axis:V,y_axis:V,z_axis:V,w_axis:V}// 4列矩阵~~~ ## 为什么放在deref.rs中 ### 设计模式Deref目标类型 这些结构体**不是直接对外暴露的类型**而是作为内部表示被其他类型通过Dereftrait访问 ~~~rust// 可能的实现模式pubstructVec3A(Vec3f32);// 对外暴露的SIMD优化版本implDerefforVec3A{typeTargetVec3f32;// 目标类型就是deref.rs中的Vec3fnderef(self)-Self::Target{self.0}}~~~ ### 文件组织逻辑-deref.rs包含被Deref的目标类型定义-这些结构体专为被Deref访问而设计-通过Deref自动获得字段访问权限 ### 使用示例 ~~~rustletvVec3A::new(1.0,2.0,3.0);println!({},v.x);// 通过Deref自动访问Vec3的字段~~~ ## 设计优势1.**性能优化**条件编译适配不同平台2.**内存控制**#[repr(C)]保证与图形API兼容3.**泛型灵活**支持不同数值类型4.**API简洁**通过Deref提供透明访问5.**内部复杂**外部类型可添加SIMD优化等特性 ~~~
【glam】deref.rs代码解析
代码内容#[cfg(not(any(feature scalar-math, target_arch spirv)))]#[repr(C)]pubstructVec3T{pubx:T,puby:T,pubz:T,}#[cfg(not(any(feature scalar-math, target_arch spirv)))]#[repr(C)]pubstructVec4T{pubx:T,puby:T,pubz:T,pubw:T,}#[cfg(not(any(feature scalar-math, target_arch spirv)))]#[repr(C)]pubstructCols2V{pubx_axis:V,puby_axis:V,}#[repr(C)]pubstructCols3V{pubx_axis:V,puby_axis:V,pubz_axis:V,}#[repr(C)]pubstructCols4V{pubx_axis:V,puby_axis:V,pubz_axis:V,pubw_axis:V,}~~~ ## 详细解释 ###1.条件编译属性 ~~~rust#[cfg(not(any(feature scalar-math, target_arch spirv)))]~~~-not(any(...))两个条件都不满足时才编译-featurescalar-math启用了scalar-math特性-target_archspirv目标架构是SPIR-VGPU着色器-只有Vec3、Vec4和Cols2有条件编译Cols3和Cols4始终可用 ###2.向量类型 ~~~rustpubstructVec3T{x:T,y:T,z:T}pubstructVec4T{x:T,y:T,z:T,w:T}~~~-#[repr(C)]保证C语言兼容的内存布局-泛型T支持不同精度类型如f32、f64-分别表示3D和4D向量的坐标分量 ###3.矩阵类型列优先存储 ~~~rustpubstructCols2V{x_axis:V,y_axis:V}// 2列矩阵pubstructCols3V{x_axis:V,y_axis:V,z_axis:V}// 3列矩阵pubstructCols4V{x_axis:V,y_axis:V,z_axis:V,w_axis:V}// 4列矩阵~~~ ## 为什么放在deref.rs中 ### 设计模式Deref目标类型 这些结构体**不是直接对外暴露的类型**而是作为内部表示被其他类型通过Dereftrait访问 ~~~rust// 可能的实现模式pubstructVec3A(Vec3f32);// 对外暴露的SIMD优化版本implDerefforVec3A{typeTargetVec3f32;// 目标类型就是deref.rs中的Vec3fnderef(self)-Self::Target{self.0}}~~~ ### 文件组织逻辑-deref.rs包含被Deref的目标类型定义-这些结构体专为被Deref访问而设计-通过Deref自动获得字段访问权限 ### 使用示例 ~~~rustletvVec3A::new(1.0,2.0,3.0);println!({},v.x);// 通过Deref自动访问Vec3的字段~~~ ## 设计优势1.**性能优化**条件编译适配不同平台2.**内存控制**#[repr(C)]保证与图形API兼容3.**泛型灵活**支持不同数值类型4.**API简洁**通过Deref提供透明访问5.**内部复杂**外部类型可添加SIMD优化等特性 ~~~