[][src]Trait ikon::decode::Decode

pub trait Decode: Sized {
    type Key: AsSize + Send + Sync;
    fn read<R: Read>(r: R) -> Result<Self>;
fn len(&self) -> usize;
fn contains_key(key: &Self::Key) -> bool;
fn get(&self, key: &Self::Key) -> Option<&Image>;
fn entries(&self) -> Iter<(Self::Key, Image)>; }

The Decode trait represents a generic icon decoder, providing methods for generating icons from byte streams, as well as functionality querying and inspecting entries.

Example

In this example we'll create a very simple Decode implementor whose keys are positive integers. First of all, we'll need a Key type:

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
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<Key, DynamicImage>
}
 
impl Decode for Icon {
    type Key = Key;
 
    fn read<R: Read>(r: R) -> io::Result<Self> {
        // Some decoding in here . . .
    }
 
    fn len(&self) -> usize {
        self.internal.len()
    }
 
    fn contains_key(key: &Self::Key) -> bool {
        self.internal.contains_key(key)
    }
 
    fn get(&self, key: &Self::Key) -> Option<&Image> {
        self.internal.get(key)
    }
 
    fn entries(&self) -> Iter<(Self::Key, Image)> {
        let output = Vec::with_capacity(self.len());
 
        for entry in self.internal {
            output.push(entry);
        }
 
        output.iter()
    }
}

Associated Types

type Key: AsSize + Send + Sync

Loading content...

Required methods

fn read<R: Read>(r: R) -> Result<Self>

Parses and loads an icon into memmory.

fn len(&self) -> usize

Returns the number of entries contained in the icon.

Example

let len = icon.len();

fn contains_key(key: &Self::Key) -> bool

Returns true if the icon includes an entry associated with key. Otherwise returns false.

Example

if icon.contains_key(&Key(32)) {
    // Do this . . .
} else {
    // Do that . . .
}

fn get(&self, key: &Self::Key) -> Option<&Image>

Returns Some(entry) if the icon includes an entry associated with key. Otherwise returns None.

Example

if let Some(entry) = icon.entry(&Key(32)) {
    // Process the entry . . .
}

fn entries(&self) -> Iter<(Self::Key, Image)>

Returns an iterator over the entries of the icon.

Example

for (key, image) in icon.entries() {
    // Do something . . .
}
Loading content...

Implementors

Loading content...