[][src]Trait favy::Encode

pub trait Encode {
    type Key: AsSize + Send + Sync;
    fn with_capacity(capacity: usize) -> Self;
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>
; 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>
, { ... } }

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

type Key: AsSize + Send + Sync

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

Returns the number of entries contained in the icon.

Example

let len = icon.len();

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>, 

Adds an individual entry to the icon.

Arguments

  • filter The resampling filter that will be used to re-scale source.
  • 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 with key.
  • Returns Err(EncodingError::Resample(_)) if the resampling filter provided in the filter argument fails produces results of dimensions other than the ones specified by key.
  • 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(())
}
Loading content...

Provided methods

fn new() -> Self

Creates a new icon.

Example

let icon = Icon::new();

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>, 

Adds a series of entries to the icon.

Arguments

  • filter The resampling filter that will be used to re-scale source.
  • 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 of keys.
  • Returns Err(EncodingError::Resample(_)) if the resampling filter provided in the filter argument fails or produces results of dimensions other than the ones specified by the items of keys.
  • 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(())
}
Loading content...

Implementors

impl Encode for Favicon[src]

type Key = Size

Loading content...