Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

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