Il mer 12 feb 2025, 18:32 Kevin Wolf <kw...@redhat.com> ha scritto: > > > + mut buf: MaybeUninit<T>, > > > > I think Rust doesn't guarantee no copies here, so maybe this could be > > Do you think that in practice the compiler won't optimise the copy away? >
It's possiblr that it does not, because it has to build the io::Result and stick the result of assume_init() in there. It all depends on the amount of inlining perhaps? I think Box<MaybeUninit>> is the only way to guarantee no copies (assume_init for Box was only stabilized recently but it can be emulated with Box::into_raw and Box::from_raw). > pub async fn read_uninit<T: SizedIoBuffer>( > > &self, > > offset: u64, > > buf: &mut MaybeUninit<T>, > > ) -> io::Result<&mut T> > > > > using assume_init_mut(). > > Are you sure that callers are ok with only getting a &mut T rather than > an owned T? > The one you have would need to be adjusted but it would work. Another possibility by the way is to have "pub async fn read_obj<T: SizedIoBuffer>(&self, offset: u64) -> io::Result<T>" and hide the usage of MaybeUninit inside the function... That one doesn't even try to avoid copies though. Paolo > Kevin > >