zeno

Trait PathData

source
pub trait PathData {
    type Commands: Iterator<Item = Command> + Clone;

    // Required method
    fn commands(&self) -> Self::Commands;

    // Provided method
    fn copy_to(&self, sink: &mut impl PathBuilder) { ... }
}
Expand description

Trait for types that represent path data.

A primary design goal for this crate is to be agnostic with regard to storage of path data. This trait provides the abstraction to make that possible.

All path data is consumed internally as an iterator over path commands and as such, this trait is similar to the IntoIterator trait, but restricted to iterators of commands and without consuming itself.

Implementations of this trait are provided for SVG path data (in the form of strings), slices/vectors of commands, and the common point and verb list structure (as the tuple (&[Point], &[Verb])).

As such, these paths are all equivalent:

use zeno::{Command, PathData, Point, Verb};

// SVG path data
let path1 = "M1,2 L3,4";

// Slice of commands
let path2 = &[
    Command::MoveTo(Point::new(1.0, 2.0)),
    Command::LineTo(Point::new(3.0, 4.0)),
][..];

// Tuple of slices to points and verbs
let path3 = (
    &[Point::new(1.0, 2.0), Point::new(3.0, 4.0)][..],
    &[Verb::MoveTo, Verb::LineTo][..],
);

assert!(path1.commands().eq(path2.commands()));
assert!(path2.commands().eq(path3.commands()));

Implementing PathData is similar to implementing IntoIterator:

use zeno::{Command, PathData};

pub struct MyPath {
    data: Vec<Command>
}

impl<'a> PathData for &'a MyPath {
    // Copied here because PathData expects Commands by value
    type Commands = std::iter::Copied<std::slice::Iter<'a, Command>>;

    fn commands(&self) -> Self::Commands {
        self.data.iter().copied()
    }
}

The provided copy_into() method evaluates the command iterator and submits the commands to a sink. You should also implement this if you have a more direct method of dispatching to a sink as rasterizer performance can be sensitive to latencies here.

Required Associated Types§

source

type Commands: Iterator<Item = Command> + Clone

Command iterator.

Required Methods§

source

fn commands(&self) -> Self::Commands

Returns an iterator over the commands described by the path data.

Provided Methods§

source

fn copy_to(&self, sink: &mut impl PathBuilder)

Copies the path data into the specified sink.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a> PathData for &'a str

source§

type Commands = SvgCommands<'a>

source§

fn commands(&self) -> Self::Commands

source§

impl<'a> PathData for &'a Vec<Command>

source§

impl<'a> PathData for &'a [Command]

source§

impl<'a> PathData for (&'a [Point], &'a [Verb])

source§

type Commands = PointsCommands<'a>

source§

fn commands(&self) -> Self::Commands

source§

fn copy_to(&self, sink: &mut impl PathBuilder)

source§

impl<T> PathData for &T
where T: PathData,

source§

type Commands = <T as PathData>::Commands

source§

fn commands(&self) -> Self::Commands

source§

fn copy_to(&self, sink: &mut impl PathBuilder)

Implementors§