From f22f9513aea21408ebf6febd01912e630e9ad5e6 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 17 Oct 2019 10:12:34 +0200 Subject: =?UTF-8?q?Add=20pagebreak=20function=20=E2=8F=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/func.rs | 66 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 27 deletions(-) (limited to 'src/func.rs') diff --git a/src/func.rs b/src/func.rs index e2391a55..eaaa476d 100644 --- a/src/func.rs +++ b/src/func.rs @@ -35,6 +35,35 @@ impl PartialEq for dyn Function { } } +/// A helper trait that describes requirements for types that can implement +/// [`Function`]. +/// +/// Automatically implemented for all types which fulfill to the bounds `Debug + +/// PartialEq + 'static`. There should be no need to implement this manually. +pub trait FunctionBounds: Debug { + /// Cast self into `Any`. + fn help_cast_as_any(&self) -> &dyn Any; + + /// Compare self with another function. + fn help_eq(&self, other: &dyn Function) -> bool; +} + +impl FunctionBounds for T +where T: Debug + PartialEq + 'static +{ + fn help_cast_as_any(&self) -> &dyn Any { + self + } + + fn help_eq(&self, other: &dyn Function) -> bool { + if let Some(other) = other.help_cast_as_any().downcast_ref::() { + self == other + } else { + false + } + } +} + /// A sequence of commands requested for execution by a function. #[derive(Debug)] pub struct CommandList<'a> { @@ -47,6 +76,11 @@ impl<'a> CommandList<'a> { CommandList { commands: vec![] } } + /// Create a command list with commands from a vector. + pub fn from_vec(commands: Vec>) -> CommandList<'a> { + CommandList { commands } + } + /// Add a command to the sequence. pub fn add(&mut self, command: Command<'a>) { self.commands.push(command); @@ -84,35 +118,13 @@ pub enum Command<'a> { AddMany(MultiLayout), SetAlignment(Alignment), SetStyle(TextStyle), + FinishLayout, } -/// A helper trait that describes requirements for types that can implement -/// [`Function`]. -/// -/// Automatically implemented for all types which fulfill to the bounds `Debug + -/// PartialEq + 'static`. There should be no need to implement this manually. -pub trait FunctionBounds: Debug { - /// Cast self into `Any`. - fn help_cast_as_any(&self) -> &dyn Any; - - /// Compare self with another function. - fn help_eq(&self, other: &dyn Function) -> bool; -} - -impl FunctionBounds for T -where T: Debug + PartialEq + 'static -{ - fn help_cast_as_any(&self) -> &dyn Any { - self - } - - fn help_eq(&self, other: &dyn Function) -> bool { - if let Some(other) = other.help_cast_as_any().downcast_ref::() { - self == other - } else { - false - } - } +macro_rules! commands { + ($($x:expr),*$(,)*) => ({ + $crate::func::CommandList::from_vec(vec![$($x,)*]) + }); } /// A map from identifiers to functions. -- cgit v1.2.3