重构模块会从编译器创建不同类型的请求?
Refactor to module creates different type request from compiler?
为什么我在重构此 example code to use a wrapper around the resize 图像库时收到不同类型的编译器请求。
基本上调整大小似乎总是 u16
输出,所以为什么我的更改要求 u8
而原始代码从来不需要此转换?
extern crate resize;
use resize::Type;
use resize::Pixel;
pub struct Resize{
w1:usize,
h1:usize,
w2:usize,
h2:usize,
src: Vec<u16>,
}
impl Resize {
pub fn new(
ow:usize,
oh:usize,
dw:usize,
dh:usize,
inc: Vec<u16>
) -> Resize {
Resize {
w1:ow,
h1:oh,
w2:dw,
h2:dh,
src:inc,
}
}
pub fn run(&self) -> Vec<u16> {
let mut dst = vec![0;self.w2*self.h2];
resize::resize(
self.w1,
self.h1,
self.w2,
self.h2,
Pixel::RGBA64,
Type::Lanczos3,
self.src.as_slice(),
&mut dst,
);
dst
}
}
编译错误信息
error[E0308]: mismatched types
--> src/main.rs:24:45
|
24 | let image = Resize::new(w1, h1, w2, h2, src);
| ^^^ expected u16, found u8
|
= note: expected type `std::vec::Vec<u16>`
found type `std::vec::Vec<u8>`
= help: here are some functions which might fulfill your needs:
- .to_vec()
error[E0308]: mismatched types
--> src/main.rs:30:54
|
30 | encoder.write_header().unwrap().write_image_data(dst).unwrap();
| ^^^ expected u8, found u16
|
= note: expected type `&[u8]`
found type `&[u16]`
TL;DR:src
变量的预期类型是 Vec<u16>
,但由于类型推断,它被设置为 Vec<u8>
。
变量 src
在行中赋值:
let mut src = vec![0;info.buffer_size()];
稍后通过调用
推断类型为Vec<u8>
reader.next_frame(&mut src).unwrap();
或
resize::resize(w1, h1, w2, h2, Gray8, Triangle, &src, &mut dst);
因为这些函数需要类型 &mut [u8]
,而不是 &mut [u16]
。
如果我们在分配 src
时显式设置预期类型 Vec<u16>
:
let mut src: Vec<u16> = vec![0; info.buffer_size()];
现在调用 next_frame()
和 resize()
将失败,因为类型不匹配,但编译器会为我们提供更好的提示,告诉我们哪里出了问题以及我们可以采取什么措施。
error[E0308]: mismatched types
--> src/main.rs:65:23
|
65 | reader.next_frame(&mut src).unwrap();
| ^^^^^^^^ expected slice, found struct `std::vec::Vec`
|
= note: expected type `&mut [u8]`
found type `&mut std::vec::Vec<u16>`
= help: here are some functions which might fulfill your needs:
- .as_mut_slice()
error[E0308]: mismatched types
--> src/main.rs:74:53
|
74 | resize::resize(w1, h1, w2, h2, Gray8, Triangle, &src, &mut dst);
| ^^^^ expected slice, found struct `std::vec::Vec`
|
= note: expected type `&[u8]`
found type `&std::vec::Vec<u16>`
= help: here are some functions which might fulfill your needs:
- .as_slice()
为什么我在重构此 example code to use a wrapper around the resize 图像库时收到不同类型的编译器请求。
基本上调整大小似乎总是 u16
输出,所以为什么我的更改要求 u8
而原始代码从来不需要此转换?
extern crate resize;
use resize::Type;
use resize::Pixel;
pub struct Resize{
w1:usize,
h1:usize,
w2:usize,
h2:usize,
src: Vec<u16>,
}
impl Resize {
pub fn new(
ow:usize,
oh:usize,
dw:usize,
dh:usize,
inc: Vec<u16>
) -> Resize {
Resize {
w1:ow,
h1:oh,
w2:dw,
h2:dh,
src:inc,
}
}
pub fn run(&self) -> Vec<u16> {
let mut dst = vec![0;self.w2*self.h2];
resize::resize(
self.w1,
self.h1,
self.w2,
self.h2,
Pixel::RGBA64,
Type::Lanczos3,
self.src.as_slice(),
&mut dst,
);
dst
}
}
编译错误信息
error[E0308]: mismatched types
--> src/main.rs:24:45
|
24 | let image = Resize::new(w1, h1, w2, h2, src);
| ^^^ expected u16, found u8
|
= note: expected type `std::vec::Vec<u16>`
found type `std::vec::Vec<u8>`
= help: here are some functions which might fulfill your needs:
- .to_vec()
error[E0308]: mismatched types
--> src/main.rs:30:54
|
30 | encoder.write_header().unwrap().write_image_data(dst).unwrap();
| ^^^ expected u8, found u16
|
= note: expected type `&[u8]`
found type `&[u16]`
TL;DR:src
变量的预期类型是 Vec<u16>
,但由于类型推断,它被设置为 Vec<u8>
。
变量 src
在行中赋值:
let mut src = vec![0;info.buffer_size()];
稍后通过调用
推断类型为Vec<u8>
reader.next_frame(&mut src).unwrap();
或
resize::resize(w1, h1, w2, h2, Gray8, Triangle, &src, &mut dst);
因为这些函数需要类型 &mut [u8]
,而不是 &mut [u16]
。
如果我们在分配 src
时显式设置预期类型 Vec<u16>
:
let mut src: Vec<u16> = vec![0; info.buffer_size()];
现在调用 next_frame()
和 resize()
将失败,因为类型不匹配,但编译器会为我们提供更好的提示,告诉我们哪里出了问题以及我们可以采取什么措施。
error[E0308]: mismatched types
--> src/main.rs:65:23
|
65 | reader.next_frame(&mut src).unwrap();
| ^^^^^^^^ expected slice, found struct `std::vec::Vec`
|
= note: expected type `&mut [u8]`
found type `&mut std::vec::Vec<u16>`
= help: here are some functions which might fulfill your needs:
- .as_mut_slice()
error[E0308]: mismatched types
--> src/main.rs:74:53
|
74 | resize::resize(w1, h1, w2, h2, Gray8, Triangle, &src, &mut dst);
| ^^^^ expected slice, found struct `std::vec::Vec`
|
= note: expected type `&[u8]`
found type `&std::vec::Vec<u16>`
= help: here are some functions which might fulfill your needs:
- .as_slice()