Use closures for QRCode::style

This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 14:51:30 +01:00
parent afd138dba6
commit 6741630218
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 16 additions and 14 deletions

View file

@ -391,9 +391,11 @@ where
/// [`QRCode`]: crate::QRCode /// [`QRCode`]: crate::QRCode
/// [`Data`]: crate::qr_code::Data /// [`Data`]: crate::qr_code::Data
#[cfg(feature = "qr_code")] #[cfg(feature = "qr_code")]
pub fn qr_code<Theme>(data: &crate::qr_code::Data) -> crate::QRCode<'_, Theme> pub fn qr_code<'a, Theme>(
data: &'a crate::qr_code::Data,
) -> crate::QRCode<'a, Theme>
where where
Theme: crate::qr_code::DefaultStyle, Theme: crate::qr_code::DefaultStyle + 'a,
{ {
crate::QRCode::new(data) crate::QRCode::new(data)
} }

View file

@ -19,23 +19,23 @@ const QUIET_ZONE: usize = 2;
/// A type of matrix barcode consisting of squares arranged in a grid which /// A type of matrix barcode consisting of squares arranged in a grid which
/// can be read by an imaging device, such as a camera. /// can be read by an imaging device, such as a camera.
#[derive(Debug)] #[allow(missing_debug_implementations)]
pub struct QRCode<'a, Theme = crate::Theme> { pub struct QRCode<'a, Theme = crate::Theme> {
data: &'a Data, data: &'a Data,
cell_size: u16, cell_size: u16,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Theme> QRCode<'a, Theme> { impl<'a, Theme> QRCode<'a, Theme> {
/// Creates a new [`QRCode`] with the provided [`Data`]. /// Creates a new [`QRCode`] with the provided [`Data`].
pub fn new(data: &'a Data) -> Self pub fn new(data: &'a Data) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self { Self {
data, data,
cell_size: DEFAULT_CELL_SIZE, cell_size: DEFAULT_CELL_SIZE,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -46,8 +46,8 @@ impl<'a, Theme> QRCode<'a, Theme> {
} }
/// Sets the style of the [`QRCode`]. /// Sets the style of the [`QRCode`].
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = style.into(); self.style = Box::new(style);
self self
} }
} }
@ -336,23 +336,23 @@ pub struct Appearance {
} }
/// The style of a [`QRCode`]. /// The style of a [`QRCode`].
pub type Style<Theme> = fn(&Theme) -> Appearance; pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>;
/// The default style of a [`QRCode`]. /// The default style of a [`QRCode`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`QRCode`]. /// Returns the default style of a [`QRCode`].
fn default_style() -> Style<Self>; fn default_style(&self) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
default default(self)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
|appearance| *appearance *self
} }
} }