@@ -137,6 +137,54 @@ where
137137 }
138138}
139139
140+ /// Trait used to allow indirect implementation of `Write` for `Cursor<Self>`.
141+ /// Since [`Cursor`] is not a foundational type, it is not possible to implement
142+ /// `Write` for `Cursor<T>` if `Write` is defined in `libcore` and `T` is in a
143+ /// downstream crate (e.g., `liballoc` or `libstd`).
144+ ///
145+ /// Methods are identical in purpose and meaning to their `Write` namesakes.
146+ trait WriteThroughCursor : Sized {
147+ fn write ( this : & mut Cursor < Self > , buf : & [ u8 ] ) -> io:: Result < usize > ;
148+ fn write_vectored ( this : & mut Cursor < Self > , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > ;
149+ fn is_write_vectored ( this : & Cursor < Self > ) -> bool ;
150+ fn write_all ( this : & mut Cursor < Self > , buf : & [ u8 ] ) -> io:: Result < ( ) > ;
151+ fn write_all_vectored ( this : & mut Cursor < Self > , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > ;
152+ fn flush ( this : & mut Cursor < Self > ) -> io:: Result < ( ) > ;
153+ }
154+
155+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
156+ impl < W : WriteThroughCursor > Write for Cursor < W > {
157+ #[ inline]
158+ fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
159+ WriteThroughCursor :: write ( self , buf)
160+ }
161+
162+ #[ inline]
163+ fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
164+ WriteThroughCursor :: write_vectored ( self , bufs)
165+ }
166+
167+ #[ inline]
168+ fn is_write_vectored ( & self ) -> bool {
169+ WriteThroughCursor :: is_write_vectored ( self )
170+ }
171+
172+ #[ inline]
173+ fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
174+ WriteThroughCursor :: write_all ( self , buf)
175+ }
176+
177+ #[ inline]
178+ fn write_all_vectored ( & mut self , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
179+ WriteThroughCursor :: write_all_vectored ( self , bufs)
180+ }
181+
182+ #[ inline]
183+ fn flush ( & mut self ) -> io:: Result < ( ) > {
184+ WriteThroughCursor :: flush ( self )
185+ }
186+ }
187+
140188// Non-resizing write implementation
141189#[ inline]
142190fn slice_write ( pos_mut : & mut u64 , slice : & mut [ u8 ] , buf : & [ u8 ] ) -> io:: Result < usize > {
@@ -348,117 +396,117 @@ impl Write for Cursor<&mut [u8]> {
348396}
349397
350398#[ stable( feature = "cursor_mut_vec" , since = "1.25.0" ) ]
351- impl < A > Write for Cursor < & mut Vec < u8 , A > >
399+ impl < A > WriteThroughCursor for & mut Vec < u8 , A >
352400where
353401 A : Allocator ,
354402{
355- fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
356- let ( pos, inner) = self . into_parts_mut ( ) ;
403+ fn write ( this : & mut Cursor < Self > , buf : & [ u8 ] ) -> io:: Result < usize > {
404+ let ( pos, inner) = this . into_parts_mut ( ) ;
357405 vec_write_all ( pos, inner, buf)
358406 }
359407
360- fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
361- let ( pos, inner) = self . into_parts_mut ( ) ;
408+ fn write_vectored ( this : & mut Cursor < Self > , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
409+ let ( pos, inner) = this . into_parts_mut ( ) ;
362410 vec_write_all_vectored ( pos, inner, bufs)
363411 }
364412
365413 #[ inline]
366- fn is_write_vectored ( & self ) -> bool {
414+ fn is_write_vectored ( _this : & Cursor < Self > ) -> bool {
367415 true
368416 }
369417
370- fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
371- let ( pos, inner) = self . into_parts_mut ( ) ;
418+ fn write_all ( this : & mut Cursor < Self > , buf : & [ u8 ] ) -> io:: Result < ( ) > {
419+ let ( pos, inner) = this . into_parts_mut ( ) ;
372420 vec_write_all ( pos, inner, buf) ?;
373421 Ok ( ( ) )
374422 }
375423
376- fn write_all_vectored ( & mut self , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
377- let ( pos, inner) = self . into_parts_mut ( ) ;
424+ fn write_all_vectored ( this : & mut Cursor < Self > , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
425+ let ( pos, inner) = this . into_parts_mut ( ) ;
378426 vec_write_all_vectored ( pos, inner, bufs) ?;
379427 Ok ( ( ) )
380428 }
381429
382430 #[ inline]
383- fn flush ( & mut self ) -> io:: Result < ( ) > {
431+ fn flush ( _this : & mut Cursor < Self > ) -> io:: Result < ( ) > {
384432 Ok ( ( ) )
385433 }
386434}
387435
388436#[ stable( feature = "rust1" , since = "1.0.0" ) ]
389- impl < A > Write for Cursor < Vec < u8 , A > >
437+ impl < A > WriteThroughCursor for Vec < u8 , A >
390438where
391439 A : Allocator ,
392440{
393- fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
394- let ( pos, inner) = self . into_parts_mut ( ) ;
441+ fn write ( this : & mut Cursor < Self > , buf : & [ u8 ] ) -> io:: Result < usize > {
442+ let ( pos, inner) = this . into_parts_mut ( ) ;
395443 vec_write_all ( pos, inner, buf)
396444 }
397445
398- fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
399- let ( pos, inner) = self . into_parts_mut ( ) ;
446+ fn write_vectored ( this : & mut Cursor < Self > , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
447+ let ( pos, inner) = this . into_parts_mut ( ) ;
400448 vec_write_all_vectored ( pos, inner, bufs)
401449 }
402450
403451 #[ inline]
404- fn is_write_vectored ( & self ) -> bool {
452+ fn is_write_vectored ( _this : & Cursor < Self > ) -> bool {
405453 true
406454 }
407455
408- fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
409- let ( pos, inner) = self . into_parts_mut ( ) ;
456+ fn write_all ( this : & mut Cursor < Self > , buf : & [ u8 ] ) -> io:: Result < ( ) > {
457+ let ( pos, inner) = this . into_parts_mut ( ) ;
410458 vec_write_all ( pos, inner, buf) ?;
411459 Ok ( ( ) )
412460 }
413461
414- fn write_all_vectored ( & mut self , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
415- let ( pos, inner) = self . into_parts_mut ( ) ;
462+ fn write_all_vectored ( this : & mut Cursor < Self > , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
463+ let ( pos, inner) = this . into_parts_mut ( ) ;
416464 vec_write_all_vectored ( pos, inner, bufs) ?;
417465 Ok ( ( ) )
418466 }
419467
420468 #[ inline]
421- fn flush ( & mut self ) -> io:: Result < ( ) > {
469+ fn flush ( _this : & mut Cursor < Self > ) -> io:: Result < ( ) > {
422470 Ok ( ( ) )
423471 }
424472}
425473
426474#[ stable( feature = "cursor_box_slice" , since = "1.5.0" ) ]
427- impl < A > Write for Cursor < Box < [ u8 ] , A > >
475+ impl < A > WriteThroughCursor for Box < [ u8 ] , A >
428476where
429477 A : Allocator ,
430478{
431479 #[ inline]
432- fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
433- let ( pos, inner) = self . into_parts_mut ( ) ;
480+ fn write ( this : & mut Cursor < Self > , buf : & [ u8 ] ) -> io:: Result < usize > {
481+ let ( pos, inner) = this . into_parts_mut ( ) ;
434482 slice_write ( pos, inner, buf)
435483 }
436484
437485 #[ inline]
438- fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
439- let ( pos, inner) = self . into_parts_mut ( ) ;
486+ fn write_vectored ( this : & mut Cursor < Self > , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
487+ let ( pos, inner) = this . into_parts_mut ( ) ;
440488 slice_write_vectored ( pos, inner, bufs)
441489 }
442490
443491 #[ inline]
444- fn is_write_vectored ( & self ) -> bool {
492+ fn is_write_vectored ( _this : & Cursor < Self > ) -> bool {
445493 true
446494 }
447495
448496 #[ inline]
449- fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
450- let ( pos, inner) = self . into_parts_mut ( ) ;
497+ fn write_all ( this : & mut Cursor < Self > , buf : & [ u8 ] ) -> io:: Result < ( ) > {
498+ let ( pos, inner) = this . into_parts_mut ( ) ;
451499 slice_write_all ( pos, inner, buf)
452500 }
453501
454502 #[ inline]
455- fn write_all_vectored ( & mut self , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
456- let ( pos, inner) = self . into_parts_mut ( ) ;
503+ fn write_all_vectored ( this : & mut Cursor < Self > , bufs : & mut [ IoSlice < ' _ > ] ) -> io:: Result < ( ) > {
504+ let ( pos, inner) = this . into_parts_mut ( ) ;
457505 slice_write_all_vectored ( pos, inner, bufs)
458506 }
459507
460508 #[ inline]
461- fn flush ( & mut self ) -> io:: Result < ( ) > {
509+ fn flush ( _this : & mut Cursor < Self > ) -> io:: Result < ( ) > {
462510 Ok ( ( ) )
463511 }
464512}
0 commit comments