BitSlice - problem defining size

Hi,

I seam to be running into problems while trying to store my BitSlice. What I have is a file that I want to read into my Vec<u8> structure and then convert it inot BitSlice so i can work with individual bits. Example:

use bitvec::prelude::*;
use std::fs::File;
use std::io::Read;

#[derive(Sized)]
pub struct XX {
    example : BitSlice,
}

impl XX{
    pub fn new()->Self {
        XX{
            example: BitSlice::empty()
        }
    }
}


fn main() {

    let mut file = File::open("example").unwrap();

    let xx = XX::new();
    let mut buffer = Vec::new();
    let total_read = file.read_to_end(&mut buffer).unwrap();
    xx.example = buffer.view_bits::<Lsb0>();

    //println!("{:?}", buffer); // looks good

    let mut i : usize = 0;
    while i < 10 {
        println!("{} {}", xx.example[i], xx.example[i+1]);
        i+=2
    }
}

the error i get:

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:10:19
   |
10 |     pub fn new()->Self {
   |                   ^^^^ doesn't have a size known at compile-time
   |
   = help: within `XX`, the trait `Sized` is not implemented for `[()]`
note: required because it appears within the type `XX`
  --> src/main.rs:5:12
   |
5  | pub struct XX {
   |            ^^
   = note: the return type of a function must have a statically known size

error[E0308]: mismatched types
  --> src/main.rs:12:22
   |
12 |             example: BitSlice::empty()
   |                      ^^^^^^^^^^^^^^^^^ expected struct `bitvec::slice::BitSlice`, found reference
   |
   = note: expected struct `bitvec::slice::BitSlice`
           found reference `&bitvec::slice::BitSlice<_, _>`

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:11:9
   |
11 | /         XX{
12 | |             example: BitSlice::empty()
13 | |         }
   | |_________^ doesn't have a size known at compile-time
   |
   = help: within `XX`, the trait `Sized` is not implemented for `[()]`
note: required because it appears within the type `XX`
  --> src/main.rs:5:12
   |
5  | pub struct XX {
   |            ^^
   = note: structs must have a statically known size to be initialized

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:22:9
   |
22 |     let xx = XX::new();
   |         ^^ doesn't have a size known at compile-time
   |
   = help: within `XX`, the trait `Sized` is not implemented for `[()]`
note: required because it appears within the type `XX`
  --> src/main.rs:5:12
   |
5  | pub struct XX {
   |            ^^
   = note: all local variables must have a statically known size
   = help: unsized locals are gated as an unstable feature

error[E0308]: mismatched types
  --> src/main.rs:25:18
   |
25 |     xx.example = *buffer.view_bits::<Lsb0>();
   |     ----------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `u8`
   |     |
   |     expected due to the type of this binding
   |
   = note: expected struct `bitvec::slice::BitSlice<usize>`
              found struct `bitvec::slice::BitSlice<u8>`

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:25:5
   |
25 |     xx.example = *buffer.view_bits::<Lsb0>();
   |     ^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: within `bitvec::slice::BitSlice`, the trait `Sized` is not implemented for `[()]`
   = note: required because it appears within the type `bitvec::slice::BitSlice`
   = note: the left-hand-side of an assignment must have a statically known size

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:22:14
   |
22 |       let xx = XX::new();
   |                ^^^^^^^ doesn't have a size known at compile-time
...
30 | /     while i < 10 {
31 | |         println!("{} {}", xx.example[i], xx.example[i+1]);
32 | |         i+=2
33 | |     }
   | |_____- this returned value is of type `()`
   |
   = help: within `XX`, the trait `Sized` is not implemented for `[()]`
note: reerror[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:10:19
   |
10 |     pub fn new()->Self {
   |                   ^^^^ doesn't have a size known at compile-time
   |
   = help: within `XX`, the trait `Sized` is not implemented for `[()]`
note: required because it appears within the type `XX`
  --> src/main.rs:5:12
   |
5  | pub struct XX {
   |            ^^
   = note: the return type of a function must have a statically known size

error[E0308]: mismatched types
  --> src/main.rs:12:22
   |
12 |             example: BitSlice::empty()
   |                      ^^^^^^^^^^^^^^^^^ expected struct `bitvec::slice::BitSlice`, found reference
   |
   = note: expected struct `bitvec::slice::BitSlice`
           found reference `&bitvec::slice::BitSlice<_, _>`

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:11:9
   |
11 | /         XX{
12 | |             example: BitSlice::empty()
13 | |         }
   | |_________^ doesn't have a size known at compile-time
   |
   = help: within `XX`, the trait `Sized` is not implemented for `[()]`
note: required because it appears within the type `XX`
  --> src/main.rs:5:12
   |
5  | pub struct XX {
   |            ^^
   = note: structs must have a statically known size to be initialized

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:22:9
   |
22 |     let xx = XX::new();
   |         ^^ doesn't have a size known at compile-time
   |
   = help: within `XX`, the trait `Sized` is not implemented for `[()]`
note: required because it appears within the type `XX`
  --> src/main.rs:5:12
   |
5  | pub struct XX {
   |            ^^
   = note: all local variables must have a statically known size
   = help: unsized locals are gated as an unstable feature

error[E0308]: mismatched types
  --> src/main.rs:25:18
   |
25 |     xx.example = *buffer.view_bits::<Lsb0>();
   |     ----------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `u8`
   |     |
   |     expected due to the type of this binding
   |
   = note: expected struct `bitvec::slice::BitSlice<usize>`
              found struct `bitvec::slice::BitSlice<u8>`

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:25:5
   |
25 |     xx.example = *buffer.view_bits::<Lsb0>();
   |     ^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: within `bitvec::slice::BitSlice`, the trait `Sized` is not implemented for `[()]`
   = note: required because it appears within the type `bitvec::slice::BitSlice`
   = note: the left-hand-side of an assignment must have a statically known size

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
  --> src/main.rs:22:14
   |
22 |       let xx = XX::new();
   |                ^^^^^^^ doesn't have a size known at compile-time
...
30 | /     while i < 10 {
31 | |         println!("{} {}", xx.example[i], xx.example[i+1]);
32 | |         i+=2
33 | |     }
   | |_____- this returned value is of type `()`
   |
   = help: within `XX`, the trait `Sized` is not implemented for `[()]`
note: required because it appears within the type `XX`
quired because it appears within the type `XX`

on the other hand if a simply run :


use bitvec::prelude::*;
use std::fs::File;
use std::io::Read;


fn main() {

    let mut file = File::open("example").unwrap();

    let mut buffer = Vec::new();
    let total_read = file.read_to_end(&mut buffer).unwrap();
    let mut xx  = BitSlice::empty();
    xx = buffer.view_bits::<Lsb0>();

    //println!("{:?}", buffer);

    let mut i : usize = 0;
    while i < 10 {
        println!("{} {}", xx[i], xx[i+1]);
        i+=2
    }
}

all works fine... How to move BitSlice into structure ? Any help ?

Thnx

A

Slices (both the primitive [T] and bitvec's BitSlice) are dynamically-sized types: they don't have a compile-time known size. (Google "rust unsized type".) Typically, slices aren't for storing data, they are most often used as views. If you want to put a growable array of bits into your own structs, use BitVec instead. Read the relevant documentation, too.

2 Likes