[−][src]Trait favy::Encode
The Encode
trait represents a generic icon encoder, providing basic
inicialization methods as well as functionality for adding entries.
Example
In this example we'll create a very simple Encode
implementor whose
keys are positive integers. First of all, we'll need a Key
type:
#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Key(pub u16); impl AsSize for Key { fn as_size(&self) -> u32 { if self.0 == 0 { 256 } else { *self.0 } } }
Note that Key(0)
represents Key(256)
. We can then implement our Icon
type:
#[derive(Clone)] pub struct Icon { internal: HashMap<u16, DynamicImage> } impl Encode for Icon { type Key = Key; fn with_capacity(capacity: usize) -> Self { Self { internal: HashMap::with_capacity(capacity) } } fn add_entry<F: FnMut(&DynamicImage, u32) -> io::Result<DynamicImage>>( &mut self, filter: F, source: &Image, key: Self::Key, ) -> Result<(), EncodingError<Self::Key>> { let size = key.as_size(); if let Entry::Vacant(entry) = self.internal.entry(size) { entry.insert(source.rasterize(filter, size)); Ok(()) } else { Err(EncodingError::AlreadyIncluded(key)) } } }
Associated Types
Loading content...Required methods
fn with_capacity(capacity: usize) -> Self
Constructs a new, empty IconEncoder
with the specified capacity.
The capacity
argument designates the number of entries
that will be allocated.
Example
let icon = Icon::with_capacity(5);
fn len(&self) -> usize
fn add_entry<F>(
&mut self,
filter: F,
source: &Image,
key: Self::Key
) -> Result<&mut Self, EncodingError<Self::Key>> where
F: FnMut(&DynamicImage, u32) -> Result<DynamicImage, Error>,
&mut self,
filter: F,
source: &Image,
key: Self::Key
) -> Result<&mut Self, EncodingError<Self::Key>> where
F: FnMut(&DynamicImage, u32) -> Result<DynamicImage, Error>,
Adds an individual entry to the icon.
Arguments
filter
The resampling filter that will be used to re-scalesource
.source
A reference to the source image this entry will be based on.key
Information on the target entry.
Return Value
- Returns
Err(EncodingError::AlreadyIncluded(_))
if the icon already contains an entry associated withkey
. - Returns
Err(EncodingError::Resample(_))
if the resampling filter provided in thefilter
argument fails produces results of dimensions other than the ones specified bykey
. - Otherwise returns
Ok(())
.
Example
fn main() -> io::Result<()> { let image = Image::open("image.svg")?; let icon = Icon::new(); icon.add_entry(resample::linear, image, Key(32))? .add_entry(resample::nearest, image, Key(64))?; Ok(()) }
Provided methods
fn new() -> Self
fn add_entries<F, I>(
&mut self,
filter: F,
source: &Image,
keys: I
) -> Result<&mut Self, EncodingError<Self::Key>> where
F: FnMut(&DynamicImage, u32) -> Result<DynamicImage, Error>,
I: IntoIterator<Item = Self::Key>,
&mut self,
filter: F,
source: &Image,
keys: I
) -> Result<&mut Self, EncodingError<Self::Key>> where
F: FnMut(&DynamicImage, u32) -> Result<DynamicImage, Error>,
I: IntoIterator<Item = Self::Key>,
Adds a series of entries to the icon.
Arguments
filter
The resampling filter that will be used to re-scalesource
.source
A reference to the source image this entry will be based on.keys
A container for the information on the target entries.
Return Value
- Returns
Err(EncodingError::AlreadyIncluded(_))
if the icon already contains an entry associated with any of the items ofkeys
. - Returns
Err(EncodingError::Resample(_))
if the resampling filter provided in thefilter
argument fails or produces results of dimensions other than the ones specified by the items ofkeys
. - Otherwise returns
Ok(())
.
Example
fn main() -> io::Result<()> { let image = Image::open("image.svg")?; let icon = Icon::new(); icon.add_entries( resample::linear, image, vec![Key(32), Key(64), Key(128)] )?; Ok(()) }