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§
Required Methods§
Provided Methods§
Sourcefn copy_to(&self, sink: &mut impl PathBuilder)
fn copy_to(&self, sink: &mut impl PathBuilder)
Copies the path data into the specified sink.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.