Skip to content

Instantly share code, notes, and snippets.

@leinlawun leinlawun/build.log
Last active Apr 18, 2017

Embed
What would you like to do?
error[E0277]: the trait bound `&mut [u8]: ReverseBuffer<E>` is not satisfied
--> mercury_buffer/src/lib.rs:754:53
|
754 | let mut buffer: &mut ReverseBuffer<E> = &mut buffer;
| ^^^^^^^^^^^ the trait `ReverseBuffer<E>` is not implemented for `&mut [u8]`
|
= help: consider adding a `where &mut [u8]: ReverseBuffer<E>` bound
= note: required for the cast to the object type `ReverseBuffer<E>`
#![no_std]
#![feature(core_intrinsics)]
use core::mem::{size_of, transmute};
use core::intrinsics::{atomic_load, atomic_store};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct BigEndian;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct LittleEndian;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Error {
OutOfBounds(usize, usize, usize),
}
pub trait ReverseBuffer<E> {
#[inline]
fn reverse_buffer(&mut self);
}
pub trait Load<O, E, D> {
#[inline]
fn load(&self, offset: O) -> Result<D, Error>;
}
pub trait Store<O, E, D> {
#[inline]
fn store(&mut self, offset: O, data: D) -> Result<(), Error>;
}
impl<B> ReverseBuffer<BigEndian> for B
where B: AsMut<[u8]>
{
fn reverse_buffer(&mut self) {
if cfg!(target_endian = "little") {
self.as_mut().reverse()
}
}
}
impl<B> ReverseBuffer<LittleEndian> for B
where B: AsMut<[u8]>
{
fn reverse_buffer(&mut self) {
if cfg!(target_endian = "big") {
self.as_mut().reverse()
}
}
}
impl<B, O, E, D> Load<O, E, D> for B
where B: AsRef<[u8]>,
O: Into<u64>
{
fn load(&self, offset: O) -> Result<D, Error> {
let buffer = self.as_ref();
let start = offset.into() as usize;
let end = start + size_of::<D>();
if buffer.len() >= end {
let buffer = &buffer[start..end];
let offset = buffer.as_ptr() as *const D;
let data = unsafe {
atomic_load(offset)
};
let mut buffer: &mut [u8] = unsafe {
transmute((&data, size_of::<D>()))
};
let mut buffer: &mut ReverseBuffer<E> = &mut buffer;
buffer.reverse_buffer();
Ok(data)
} else {
Err(Error::OutOfBounds(buffer.len(), start, end))
}
}
}
impl<B, O, E, D> Store<O, E, D> for B
where B: AsMut<[u8]>,
O: Into<u64>
{
fn store(&mut self, offset: O, data: D) -> Result<(), Error> {
let buffer = self.as_mut();
let start = offset.into() as usize;
let end = start + size_of::<D>();
if buffer.len() >= end {
let buffer = &mut buffer[start..end];
let offset = buffer.as_ptr() as *mut D;
let mut buffer: &mut [u8] = unsafe {
transmute((&data, size_of::<D>()))
};
let mut buffer: &mut ReverseBuffer<E> = &mut buffer;
buffer.reverse_buffer();
unsafe {
atomic_store(offset, data)
}
Ok(())
} else {
Err(Error::OutOfBounds(buffer.len(), start, end))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.