diff --git a/wgpu-types/src/into_cow.rs b/wgpu-types/src/into_cow.rs new file mode 100644 index 000000000..b9704aa03 --- /dev/null +++ b/wgpu-types/src/into_cow.rs @@ -0,0 +1,45 @@ +use std::borrow::Cow; + +/// Implements conversion into a cow. +pub trait IntoCow<'c, CowType: ?Sized + ToOwned> { + fn into_cow(self) -> Cow<'c, CowType>; +} + +impl<'c, T: Clone> IntoCow<'c, [T]> for &'c [T] { + fn into_cow(self) -> Cow<'c, [T]> { + Cow::Borrowed(self) + } +} + +impl<'c> IntoCow<'c, str> for &'c str { + fn into_cow(self) -> Cow<'c, str> { + Cow::Borrowed(self) + } +} + +impl IntoCow<'static, [T]> for Vec { + fn into_cow(self) -> Cow<'static, [T]> { + Cow::Owned(self) + } +} + +impl IntoCow<'static, str> for String { + fn into_cow(self) -> Cow<'static, str> { + Cow::Owned(self) + } +} + +macro_rules! into_cow_array { + ($($number:literal),*) => {$( + impl<'c, T: Clone> IntoCow<'c, [T]> for &'c [T; $number] { + fn into_cow(self) -> Cow<'c, [T]> { + Cow::Borrowed(&self[..]) + } + } + )*}; +} + +into_cow_array!( + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27, 28, 29, 30, 31, 32 +); diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index ea097d80c..c83738765 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -12,6 +12,9 @@ use serde::{Deserialize, Serialize}; use std::{borrow::Cow, num::NonZeroU32, ops::Range}; +pub use into_cow::IntoCow; +mod into_cow; + /// Integral type used for buffer offsets. pub type BufferAddress = u64; /// Integral type used for buffer slice sizes. @@ -1305,6 +1308,42 @@ impl TextureDescriptor { } } +/// Texture descriptor builder methods +impl<'a> TextureDescriptor>> { + pub fn new( + size: Extent3d, + dimension: TextureDimension, + format: TextureFormat, + usage: TextureUsage, + ) -> Self { + Self { + label: None, + size, + mip_level_count: 1, + sample_count: 1, + dimension, + format, + usage, + } + } + + pub fn label(&mut self, label: impl IntoCow<'a, str>) -> &mut Self { + self.label = Some(label.into_cow()); + self + } + + pub fn mip_level_count(&mut self, count: u32) -> &mut Self { + // TODO: validate this size is sane + self.mip_level_count = count; + self + } + + pub fn sample_count(&mut self, count: u32) -> &mut Self { + self.sample_count = count; + self + } +} + /// Kind of data the texture holds. #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]