연습 문제로 비트 벡터 클래스를 구현하는 중일 뿐이지 만 다음 주 코드로 문제가 생기는 일주일도 안되는 녹을 알고 있습니다. :비트 벡터에 제네릭을 사용할 때 이진 연산! = 적용 할 수 없음
use std::cmp::Eq;
use std::ops::BitAnd;
use std::ops::Index;
use std::ops::Not;
struct BitVector<S = usize>
where S: Sized + BitAnd<usize> + Not + Eq {
data: Vec<S>,
capacity: usize
}
impl<S> BitVector<S>
where S: Sized + BitAnd<usize> + Not + Eq {
fn with_capacity(capacity: usize) -> BitVector {
let len = (capacity/(std::mem::size_of::<S>() * 8)) + 1;
BitVector { data: vec![0; len], capacity: capacity }
}
}
impl<S> Index<usize> for BitVector<S>
where S: Sized + BitAnd<usize> + Not + Eq {
type Output = bool;
fn index(&self, index: usize) -> &bool {
let data_index = index/(std::mem::size_of::<S>() * 8);
let remainder = index % (std::mem::size_of::<S>() * 8);
(self.data[data_index] & (1 << remainder)) != 0
}
}
아이디어 S
가 with_capacity
에 0
로 설정하면 모두 0으로 구성 S
대한 비트 값을 생성하도록, 예 u8
, u16
, u32
, u64
들면 하나 usize
될 수 있다는 것이다. 여기
lib.rs:27:10: 27:50 error: binary operation
!=
cannot be applied to type<S as std::ops::BitAnd<usize>>::Output
[E0369]
lib.rs:27 (self.data[data_index] & (1 << remainder)) != 0
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:27:10: 27:50 help: runrustc --explain E0369
to see a detailed explanation
lib.rs:27:10: 27:50 note: an implementation ofstd::cmp::PartialEq
might be missing for<S as std::ops::BitAnd<usize>>::Output
lib.rs:27 (self.data[data_index] & (1 << remainder)) != 0 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
error: Could not compilebit-vector
.
하지만,'인덱스 :: 당신은 즉시 그것을 계산할 수 있도록'& Output'을 반환 index'. .. –