Skip to content

Commit

Permalink
Unify methods by leveraging Into<Cow> in image and svg
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Nov 5, 2022
1 parent 6ce12fc commit bc5986c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
28 changes: 5 additions & 23 deletions native/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,15 @@ impl Handle {
/// pixels.
///
/// This is useful if you have already decoded your image.
pub fn from_pixels(width: u32, height: u32, pixels: Vec<u8>) -> Handle {
Self::from_data(Data::Pixels {
width,
height,
pixels: Cow::Owned(pixels),
})
}

/// Like [`Handle::from_pixels`], but from static pixel data.
///
/// Useful for images included in binary, for instance with [`include_bytes!`].
pub fn from_static_pixels(
pub fn from_pixels(
width: u32,
height: u32,
pixels: &'static [u8],
pixels: impl Into<Cow<'static, [u8]>>,
) -> Handle {
Self::from_data(Data::Pixels {
width,
height,
pixels: Cow::Borrowed(pixels),
pixels: pixels.into(),
})
}

Expand All @@ -55,15 +44,8 @@ impl Handle {
///
/// This is useful if you already have your image loaded in-memory, maybe
/// because you downloaded or generated it procedurally.
pub fn from_memory(bytes: Vec<u8>) -> Handle {
Self::from_data(Data::Bytes(Cow::Owned(bytes)))
}

/// Like [`Handle::from_memory`], but from static image data.
///
/// Useful for images included in binary, for instance with [`include_bytes!`].
pub fn from_static_memory(bytes: &'static [u8]) -> Handle {
Self::from_data(Data::Bytes(Cow::Borrowed(bytes)))
pub fn from_memory(bytes: impl Into<Cow<'static, [u8]>>) -> Handle {
Self::from_data(Data::Bytes(bytes.into()))
}

fn from_data(data: Data) -> Handle {
Expand Down
11 changes: 2 additions & 9 deletions native/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ impl Handle {
///
/// This is useful if you already have your SVG data in-memory, maybe
/// because you downloaded or generated it procedurally.
pub fn from_memory(bytes: impl Into<Vec<u8>>) -> Handle {
Self::from_data(Data::Bytes(Cow::Owned(bytes.into())))
}

/// Like [`Handle::from_memory`], but from static image data.
///
/// Useful for images included in binary, for instance with [`include_bytes!`].
pub fn from_static_memory(bytes: &'static [u8]) -> Handle {
Self::from_data(Data::Bytes(Cow::Borrowed(bytes)))
pub fn from_memory(bytes: impl Into<Cow<'static, [u8]>>) -> Handle {
Self::from_data(Data::Bytes(bytes.into()))
}

fn from_data(data: Data) -> Handle {
Expand Down

0 comments on commit bc5986c

Please sign in to comment.