Sono in procinto di implementare una classe Bit Vector come esercizio, tuttavia solo conoscendo Rust per meno di una settimana mi trovo nei guai con il seguente codice :Operazione binaria! = Non può essere applicata quando si usano i generici per un vettore bit
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
}
}
L'idea è che S
può essere uno per esempio u8
, u16
, u32
, u64
e usize
garantire che impostandola 0
in with_capacity
crea un valore di bit per S
che consiste di tutti zeri.
L'errore che ottengo è il seguente:
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
.
Non voglio a piovere sulla tua parata, ma 'Indice :: index' restituisce un' & Output' quindi non si può calcolare al volo. .. –