From 849588a96bf894a81640938b0aa188dd8a11efe4 Mon Sep 17 00:00:00 2001 From: Bauke Date: Thu, 25 Jan 2024 19:57:44 +0100 Subject: [PATCH] Make GeglOperation object-safe by implementing Default in a roundabout way. --- gegl/source/lib.rs | 15 +++++++++++++-- gegl/source/operations/macros.rs | 4 ++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/gegl/source/lib.rs b/gegl/source/lib.rs index 7b477ad..9c8301a 100644 --- a/gegl/source/lib.rs +++ b/gegl/source/lib.rs @@ -15,16 +15,27 @@ pub type GeglData = indexmap::IndexMap<&'static str, String>; /// The [`GeglOperation`] trait defines a set of common functions for the /// individual operations to implement so they can be used with the GEGL CLI. -pub trait GeglOperation: Default + std::fmt::Debug { +pub trait GeglOperation: std::fmt::Debug { /// Some GEGL operations will run infinitely unless you limit the buffer in /// some way, so all operations must indicate whether or not they should be /// followed by a crop operation. fn append_crop_operation(&self) -> bool; + /// Return the default values for the operation as a [`GeglData`]. + /// + /// The reason this can't use [`Default`] is because we want the trait to be + /// object-safe so we can use [`Box`]. Implementing + /// [`Default`] makes the trait [`Sized`] and no longer object-safe. + /// + /// The [`gegl_operation`] macro still implements and calls [`Default`] anyway + /// so it's easy to instantiate operations but we have to go in a roundabout + /// way to actually get the values from it for this function. + fn default_values(&self) -> GeglData; + /// Creates the parameters for the graph to be used with the GEGL CLI. fn graph(&self, include_default_values: bool) -> Vec { let mut graph = vec![self.name().to_string()]; - let defaults = Self::default().values(); + let defaults = self.default_values(); for (key, value) in self.values() { if !include_default_values && defaults.get(key) == Some(&value) { diff --git a/gegl/source/operations/macros.rs b/gegl/source/operations/macros.rs index f666c62..4afb976 100644 --- a/gegl/source/operations/macros.rs +++ b/gegl/source/operations/macros.rs @@ -31,6 +31,10 @@ macro_rules! gegl_operation { $append_crop } + fn default_values(&self) -> $crate::GeglData { + Self::default().values() + } + fn name(&self) -> &'static str { concat!("gegl:", $gegl_name) }