Я пытаюсь утверждать, что два среза равны, но один из срезов интерпретируется как массив:
#[derive(Debug, PartialEq)]
enum Error {
TooBig,
}
type Bytes = [u8];
struct Fixed {
length: u32,
}
impl<'a> Fixed {
pub fn new(length: u32) -> Fixed {
Fixed { length: length }
}
pub fn length(&self) -> u32 {
self.length
}
pub fn encode(&self, decoded: &'a Bytes) -> Result<&'a Bytes, Error> {
if decoded.len() > self.length() as usize {
Err(Error::TooBig)
} else {
Ok(&decoded)
}
}
pub fn decode(&self, decoded: &'a Bytes) -> Result<&'a Bytes, Error> {
if decoded.len() > self.length() as usize {
Err(Error::TooBig)
} else {
Ok(&decoded)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixed_0() {
let length = 0;
let fixed = Fixed::new(length);
let encoded = [];
let decoded = [];
assert_eq!(fixed.length(), length);
assert_eq!(fixed.encode(&decoded), Ok(&encoded));
assert_eq!(fixed.decode(&encoded), Ok(&decoded));
assert_eq!(fixed.encode(&[1]), Err(Error::TooBig));
}
#[test]
fn fixed_1() {
let length = 1;
let fixed = Fixed::new(length);
let encoded: [u8; 1] = [1];
let decoded: [u8; 1] = [1];
assert_eq!(fixed.length(), length);
assert_eq!(fixed.encode(&decoded).unwrap(), &encoded);
assert_eq!(fixed.decode(&encoded).unwrap(), &decoded);
}
}
суть
А вот и мои ошибки:
error[E0308]: mismatched types
--> src/lib.rs:49:9
|
49 | assert_eq!(fixed.encode(&decoded), Ok(&encoded));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice, found array of 0 elements
|
= note: expected type `std::result::Result<&[u8], Error>`
found type `std::result::Result<&[_; 0], _>`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0308]: mismatched types
--> src/lib.rs:50:9
|
50 | assert_eq!(fixed.decode(&encoded), Ok(&decoded));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice, found array of 0 elements
|
= note: expected type `std::result::Result<&[u8], Error>`
found type `std::result::Result<&[u8; 0], _>`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
Мое чтение документов приводит меня к мысли, что с помощью &
я должен создать срез. Чего мне не хватает?