Merge pull request #321 from image-rs/forward-tiff-value-for-vec-and-array
Add diagnostic helps for TiffValue proxy implementations
This commit is contained in:
4 files changed
+44
-7
No files matched your search
@@ -9,14 +9,14 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
rust: ["1.74.0", stable, beta, nightly]
|
rust: ["1.85.0", stable, beta, nightly]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- uses: dtolnay/rust-toolchain@nightly
|
- uses: dtolnay/rust-toolchain@nightly
|
||||||
if: ${{ matrix.rust == '1.74.0' }}
|
if: ${{ matrix.rust == '1.85.0' }}
|
||||||
- name: Generate Cargo.lock with minimal-version dependencies
|
- name: Generate Cargo.lock with minimal-version dependencies
|
||||||
if: ${{ matrix.rust == '1.74.0' }}
|
if: ${{ matrix.rust == '1.85.0' }}
|
||||||
run: cargo -Zminimal-versions generate-lockfile
|
run: cargo -Zminimal-versions generate-lockfile
|
||||||
|
|
||||||
- uses: dtolnay/rust-toolchain@v1
|
- uses: dtolnay/rust-toolchain@v1
|
||||||
@@ -29,7 +29,7 @@ jobs:
|
|||||||
- name: build
|
- name: build
|
||||||
run: cargo build -v
|
run: cargo build -v
|
||||||
- name: test
|
- name: test
|
||||||
if: ${{ matrix.rust != '1.74.0' }}
|
if: ${{ matrix.rust != '1.85.0' }}
|
||||||
run: cargo test -v && cargo doc -v
|
run: cargo test -v && cargo doc -v
|
||||||
|
|
||||||
features:
|
features:
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ edition = "2021"
|
|||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
# note: when changed, also update test runner in `.github/workflows/rust.yml`
|
# note: when changed, also update test runner in `.github/workflows/rust.yml`
|
||||||
rust-version = "1.74.0"
|
rust-version = "1.85.0"
|
||||||
|
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "TIFF decoding and encoding library in pure Rust"
|
description = "TIFF decoding and encoding library in pure Rust"
|
||||||
|
|||||||
+2
-2
@@ -766,7 +766,7 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind> ImageEncoder<'a, W, T,
|
|||||||
} else {
|
} else {
|
||||||
let mut sample_format: Vec<_> = <T>::BITS_PER_SAMPLE.to_vec();
|
let mut sample_format: Vec<_> = <T>::BITS_PER_SAMPLE.to_vec();
|
||||||
let replicated =
|
let replicated =
|
||||||
core::iter::repeat(<T>::BITS_PER_SAMPLE[0]).take(self.extra_samples.len());
|
core::iter::repeat_n(<T>::BITS_PER_SAMPLE[0], self.extra_samples.len());
|
||||||
sample_format.extend(replicated);
|
sample_format.extend(replicated);
|
||||||
|
|
||||||
self.encoder
|
self.encoder
|
||||||
@@ -788,7 +788,7 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind> ImageEncoder<'a, W, T,
|
|||||||
// extra samples and not provided via a slice used for samples themselves.
|
// extra samples and not provided via a slice used for samples themselves.
|
||||||
.unwrap_or(SampleFormat::Void.to_u16());
|
.unwrap_or(SampleFormat::Void.to_u16());
|
||||||
|
|
||||||
sample_format.extend(core::iter::repeat(extra_format).take(self.extra_samples.len()));
|
sample_format.extend(core::iter::repeat_n(extra_format, self.extra_samples.len()));
|
||||||
|
|
||||||
self.encoder
|
self.encoder
|
||||||
.write_tag(Tag::SampleFormat, &sample_format[..])?;
|
.write_tag(Tag::SampleFormat, &sample_format[..])?;
|
||||||
|
|||||||
@@ -5,6 +5,14 @@ use crate::{bytecast, tags::Type, TiffError, TiffFormatError, TiffResult};
|
|||||||
use super::writer::TiffWriter;
|
use super::writer::TiffWriter;
|
||||||
|
|
||||||
/// Trait for types that can be encoded in a tiff file
|
/// Trait for types that can be encoded in a tiff file
|
||||||
|
#[diagnostic::on_unimplemented(
|
||||||
|
message = "the type `{Self}` does not implement `TiffValue`",
|
||||||
|
note = "the trait is implemented for primitive types (`u8`, `i16`, `f32`, etc.)",
|
||||||
|
note = "the trait is implemented for shared references to values",
|
||||||
|
note = "the trait is implemented for slices, pass them by reference (e.g. `&[u8]`)",
|
||||||
|
note = "the trait is implemented for arrays if it is implemented for a slice",
|
||||||
|
note = "values in a `Vec` or `Box` should be dereferenced, e.g. `&vec[..]`"
|
||||||
|
)]
|
||||||
pub trait TiffValue {
|
pub trait TiffValue {
|
||||||
const BYTE_LEN: u8;
|
const BYTE_LEN: u8;
|
||||||
const FIELD_TYPE: Type;
|
const FIELD_TYPE: Type;
|
||||||
@@ -450,6 +458,14 @@ impl TiffValue for str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If you pass `&Vec<_>` then you'd get at first sight the complaint:
|
||||||
|
//
|
||||||
|
// `Vec<T>` does not implement `TiffValue`
|
||||||
|
//
|
||||||
|
// and a list of implementations of `TiffValue` that are quite unrelated. That is not very helpful.
|
||||||
|
// We do not *want* `Vec` to implement the trait as an owning type, instead you should pass a
|
||||||
|
// reference to a slice. The error message is further customized in the diagnostics of the trait.
|
||||||
|
#[diagnostic::do_not_recommend]
|
||||||
impl<T: TiffValue + ?Sized> TiffValue for &'_ T {
|
impl<T: TiffValue + ?Sized> TiffValue for &'_ T {
|
||||||
const BYTE_LEN: u8 = T::BYTE_LEN;
|
const BYTE_LEN: u8 = T::BYTE_LEN;
|
||||||
const FIELD_TYPE: Type = T::FIELD_TYPE;
|
const FIELD_TYPE: Type = T::FIELD_TYPE;
|
||||||
@@ -467,6 +483,27 @@ impl<T: TiffValue + ?Sized> TiffValue for &'_ T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An array is treated like a slice of the same length.
|
||||||
|
impl<T, const N: usize> TiffValue for [T; N]
|
||||||
|
where
|
||||||
|
[T]: TiffValue,
|
||||||
|
{
|
||||||
|
const BYTE_LEN: u8 = <[T]>::BYTE_LEN;
|
||||||
|
const FIELD_TYPE: Type = <[T]>::FIELD_TYPE;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.as_slice().count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
self.as_slice().write(writer)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn data(&self) -> Cow<'_, [u8]> {
|
||||||
|
self.as_slice().data()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! impl_tiff_value_for_contiguous_sequence {
|
macro_rules! impl_tiff_value_for_contiguous_sequence {
|
||||||
($inner_type:ty; $bytes:expr; $field_type:expr) => {
|
($inner_type:ty; $bytes:expr; $field_type:expr) => {
|
||||||
impl $crate::encoder::TiffValue for [$inner_type] {
|
impl $crate::encoder::TiffValue for [$inner_type] {
|
||||||
|
|||||||
Reference in new issue
Block a user