This secondo call to writer.write_simple generates an error ("cannot borrow writer as mutable more than once at a time").
The commented part works instead.
Why is that? Where is writer borrowed?
By trial and error i found that the by removing the lifetime annotation ('b) for &mut self the error disappers but then i'm not able to modify self.0 anymore.
I could just uuse the other methods but still i would like to understand the reason of this behaviour.
When using mutable references, you need to keep separate the lifetimes for “a Self is borrowed” (&'a mut self) and “Self contains borrowed data” (Self = DcdWriter<'a>). Creating an &'a mut DcdWriter<'a>, with the same lifetime in both positions, is nearly always wrong, because it implies that the DcdWriter is now exclusively borrowed for the rest of its existence, and therefore it can no longer be used at all.
Remove all of your extra lifetime annotations. Once you do, you'll have a problem with your assignment to self.0. The spcific trick to solve this problem is that you need to take (move) the existing self.0 mutable reference, so that you can slice it and get back one with the original 'a lifetime instead of a reborrow through the &mut self reference, which necessarily has a shorter lifetime.
However, &mut is not just mutable. It's a special kind of loan that is very very strictly exclusive, meaning there can't be any other &mut to the same data anywhere, under any circumstances, even for a split second.
self.0 = &mut self.0[len..];
but here self.0 and &mut self.0[len..] coexist for a brief moment (it calls self.0.index_mut() -> &mut under the hood).
There is a silly workaround for this:
// self.0 is replaced with an empty `&mut []` to nowhere
let old_ref = std::mem::take(&mut self.0);
self.0 = &mut old_ref[len..]; // or old_ref.split_at_mut(len).0