8

私は演習として Bit Vector クラスを実装している最中ですが、Rust を 1 週間も知らずにしか知りませんでした。次のコードで問題が発生しました。

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、たとえばu8u16u32のいずれかu64にすることができ、 in にusize設定すると、すべてゼロで構成される のビット値が作成されるようにすることです。0with_capacityS

私が得るエラーは次のとおりです。

lib.rs:27:10: 27:50 エラー: 二項演算はタイプ[E0369]!=に適用できません lib.rs:27 (self.data[data_index] & (1 << 残り)) != 0 ^~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lib.rs:27:10: 27:50 ヘルプ: 実行詳細な説明を見るには lib.rs:27:10: 27:50 注: lib.rs:27 の実装が欠落している可能性があります (self.data[data_index] & (1 << 残り)) != 0 ^~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error error: Could not compile .<S as std::ops::BitAnd<usize>>::Output


rustc --explain E0369
std::cmp::PartialEq<S as std::ops::BitAnd<usize>>::Output


bit-vector

4

1 に答える 1