Skip to content
This repository was archived by the owner on May 23, 2024. It is now read-only.

ices/108329.rs: fixed with errors #1521

Merged
merged 1 commit into from
Mar 4, 2023
Merged

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Mar 4, 2023

Issue: rust-lang/rust#108329

#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

pub enum Restriction<const V: bool = true> {}
pub trait Valid {}

impl Valid for Restriction<true> {}

/**
    a circular buffer
*/
pub struct CircularBuffer<T, const S: usize> {
    buffer: Vec<T>,
    index: usize,
}

impl<T, const S: usize> CircularBuffer<T, { S }>
where
    Restriction<{ S > 0 }>: Valid,
{
    /**
        create a new [`CircularBuffer`] instance
    */
    pub fn new() -> Self {
        Self {
            buffer: Vec::with_capacity(S),
            index: S - 1,
        }
    }

    /**
        push a value onto the buffer
    */
    pub fn push(&mut self, value: T) {
        self.index = self.index.checked_add(1).unwrap_or(0) % S;
        match self.count() < self.capacity() {
            true => self.buffer.insert(self.index, value),
            false => self.buffer[self.index] = value,
        }
    }

    /**
        push a value onto the buffer
    */
    pub fn pop(&mut self) -> Result<T, CircularBufferError> {
        if self.count() == 0 {
            Err(CircularBufferError::Underflow)?
        }

        let old = self.buffer.remove(self.index);
        self.index = self.index.checked_sub(1).unwrap_or(S - 1);
        Ok(old)
    }
}

impl<T, const S: usize> CircularBuffer<T, { S }> {
    /**
        returns the current count of the buffer
    */
    pub fn count(&self) -> usize {
        self.buffer.len()
    }

    /**
        returns the max capacity of the buffer
    */
    pub const fn capacity(&self) -> usize {
        S
    }
}

impl<T, const S: usize> CircularBuffer<T, { S }>
where
    T: Clone,
{
    /**
        take the values in a vec, leaving an empty buffer
    */
    pub fn take(&mut self) -> Vec<T> {
        let mut buf = Vec::with_capacity(self.buffer.len());
        buf.copy_from_slice(self.buffer[..]);
        self.index = S - 1;
        buf
    }

    /**
        returns true if the buffer is empty
    */
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }
}

#[derive(Debug, PartialEq)]
pub enum CircularBufferError {
    /// attempted to pop when buffer is empty
    Underflow,
}

fn main() {}
=== stdout ===
=== stderr ===
error[E0308]: mismatched types
  --> /home/runner/work/glacier/glacier/ices/108329.rs:81:29
   |
81 |         buf.copy_from_slice(self.buffer[..]);
   |             --------------- ^^^^^^^^^^^^^^^
   |             |               |
   |             |               expected `&[_]`, found `[T]`
   |             |               help: consider borrowing here: `&self.buffer[..]`
   |             arguments to this method are incorrect
   |             here the type of `buf` is inferred to be `[_]`
   |
   = note: expected reference `&[_]`
                  found slice `[T]`
note: method defined here
  --> /rustc/44cfafe2fafe816395d3acc434663a45d5178c41/library/core/src/slice/mod.rs:3325:12

error[E0277]: the trait bound `T: Copy` is not satisfied
  --> /home/runner/work/glacier/glacier/ices/108329.rs:81:13
   |
81 |         buf.copy_from_slice(self.buffer[..]);
   |             ^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T`
   |
note: required by a bound in `core::slice::<impl [T]>::copy_from_slice`
  --> /rustc/44cfafe2fafe816395d3acc434663a45d5178c41/library/core/src/slice/mod.rs:3325:5
help: consider further restricting this bound
   |
74 |     T: Clone + std::marker::Copy,
   |              +++++++++++++++++++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
==============

=== stdout ===
=== stderr ===
error[E0308]: mismatched types
  --> /home/runner/work/glacier/glacier/ices/108329.rs:81:29
   |
81 |         buf.copy_from_slice(self.buffer[..]);
   |             --------------- ^^^^^^^^^^^^^^^
   |             |               |
   |             |               expected `&[_]`, found `[T]`
   |             |               help: consider borrowing here: `&self.buffer[..]`
   |             arguments to this method are incorrect
   |             here the type of `buf` is inferred to be `[_]`
   |
   = note: expected reference `&[_]`
                  found slice `[T]`
note: method defined here
  --> /rustc/44cfafe2fafe816395d3acc434663a45d5178c41/library/core/src/slice/mod.rs:3325:12

error[E0277]: the trait bound `T: Copy` is not satisfied
  --> /home/runner/work/glacier/glacier/ices/108329.rs:81:13
   |
81 |         buf.copy_from_slice(self.buffer[..]);
   |             ^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `T`
   |
note: required by a bound in `core::slice::<impl [T]>::copy_from_slice`
  --> /rustc/44cfafe2fafe816395d3acc434663a45d5178c41/library/core/src/slice/mod.rs:3325:5
help: consider further restricting this bound
   |
74 |     T: Clone + std::marker::Copy,
   |              +++++++++++++++++++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
==============
@Alexendoo Alexendoo merged commit 8e17699 into master Mar 4, 2023
@Alexendoo Alexendoo deleted the autofix/ices/108329.rs branch March 4, 2023 11:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants