Skip to content

Instantly share code, notes, and snippets.

@SiegeLord
Created March 8, 2014 00:28
Show Gist options
  • Select an option

  • Save SiegeLord/9423147 to your computer and use it in GitHub Desktop.

Select an option

Save SiegeLord/9423147 to your computer and use it in GitHub Desktop.
Mutable hacks
struct S<T>
{
data: T
}
trait Mut<'l>
{
fn set(self, idx: uint, val: u8);
}
trait Common<'l>
{
fn get(self) -> &'l [u8];
fn len(self) -> uint
{
self.get().len()
}
}
impl<'l> Mut<'l> for &'l mut S<&'l mut [u8]>
{
fn set(self, idx: uint, val: u8)
{
self.data[idx] = val;
}
}
impl<'l> Common<'l> for &'l S<&'l mut [u8]>
{
fn get(self) -> &'l [u8]
{
self.data.as_slice()
}
}
impl<'l> Common<'l> for &'l S<&'l [u8]>
{
fn get(self) -> &'l [u8]
{
self.data
}
}
fn main()
{
let mut s = S{ data: &mut [0u8] };
{
let rs = &s;
rs.len();
}
s.set(0, 5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment