From ec0477125f4ff968e3c566db471ef58bc2be8f78 Mon Sep 17 00:00:00 2001 From: Andrew Champion Date: Sat, 1 Feb 2020 18:25:05 +0000 Subject: [PATCH] Tags: restore primitive conversions as inherent methods --- src/decoder/mod.rs | 2 +- src/encoder/mod.rs | 2 +- src/tags.rs | 51 +++++++++++++++++++++------------------------- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 9d06c31..4836c32 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -6,7 +6,7 @@ use std::cmp; use {ColorType, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError}; use self::ifd::Directory; -use tags::{CompressionMethod, PhotometricInterpretation, Predictor, Tag, Type, U16Tag}; +use tags::{CompressionMethod, PhotometricInterpretation, Predictor, Tag, Type}; use self::stream::{ByteOrder, EndianReader, LZWReader, DeflateReader, PackBitsReader, SmartReader}; diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 794b298..22e39d3 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -4,7 +4,7 @@ use std::convert::TryFrom; use std::io::{Seek, Write}; use std::mem; -use tags::{self, ResolutionUnit, Tag, Type, U16Tag}; +use tags::{self, ResolutionUnit, Tag, Type}; use error::{TiffError, TiffFormatError, TiffResult}; pub mod colortype; diff --git a/src/tags.rs b/src/tags.rs index 3c074cd..fabb74d 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -1,32 +1,8 @@ -mod seals { - pub trait TagConversion: Sized { - fn __from_inner_type(T) -> Result; - - fn __to_inner_type(&self) -> T; - } -} - -pub trait U16Tag: Sized { - fn from_u16(val: u16) -> Option; - - fn to_u16(&self) -> u16; -} - -impl> U16Tag for T { - fn from_u16(val: u16) -> Option { - Self::__from_inner_type(val).ok() - } - - fn to_u16(&self) -> u16 { - Self::__to_inner_type(self) - } -} - macro_rules! tags { { // Permit arbitrary meta items, which include documentation. $( #[$enum_attr:meta] )* - $vis:vis enum $name:ident($ty:ty) $(unknown($unknown_doc:literal))* { + $vis:vis enum $name:ident($ty:tt) $(unknown($unknown_doc:literal))* { // Each of the `Name = Val,` permitting documentation. $($(#[$ident_attr:meta])* $tag:ident = $val:expr,)* } @@ -38,13 +14,13 @@ macro_rules! tags { // FIXME: switch to non_exhaustive once stabilized and compiler requirement new enough #[doc(hidden)] __NonExhaustive, - $( + $( #[doc = $unknown_doc] Unknown($ty), )* } - impl ::tags::seals::TagConversion<$ty> for $name { + impl $name { #[inline(always)] fn __from_inner_type(n: $ty) -> Result { match n { @@ -62,7 +38,26 @@ macro_rules! tags { } } } - } + + tags!($name, $ty); + }; + // For u16 tags, provide direct inherent primitive conversion methods. + ($name:tt, u16) => { + impl $name { + #[inline(always)] + pub fn from_u16(val: u16) -> Option { + Self::__from_inner_type(val).ok() + } + + #[inline(always)] + pub fn to_u16(&self) -> u16 { + Self::__to_inner_type(self) + } + } + }; + // For other tag types, do nothing for now. With concat_idents one could + // provide inherent conversion methods for all types. + ($name:tt, $ty:tt) => {}; } // Note: These tags appear in the order they are mentioned in the TIFF reference