[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <d7f045c1-79df-4592-a116-01874f402de4@redhat.com>
Date: Thu, 28 Nov 2024 17:43:43 +0100
From: Paolo Bonzini <pbonzini@...hat.com>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
boqun.feng@...il.com, ojeda@...nel.org, benno.lossin@...ton.me,
axboe@...nel.dk, tmgross@...ch.edu, bjorn3_gh@...tonmail.com,
gary@...yguo.net, alex.gaynor@...il.com, a.hindborg@...nel.org
Subject: Re: [PATCH 1/2] rust: Zeroable: allow struct update syntax outside
init macros
On 11/28/24 15:40, Alice Ryhl wrote:
>> The definition of the ZERO constant requires adding a Sized boundary, but
>> this is not a problem either because neither slices nor trait objects
>> are zeroable.
>>
>> Signed-off-by: Paolo Bonzini <pbonzini@...hat.com>
>
> Slices are zeroable. I know they don't implement the trait,
Right, I should have used the uppercase "Zeroable" for clarity.
> but they could implement it, and this could be used to implement e.g.:
>
> pub fn write_zero<T: Zeroed + ?Sized>(value: &mut T) {
> memset(0, ...);
> }
Yeah, that would be I think
pub fn write_zero<T: Zeroable + ?Sized>(value: &mut T) {
unsafe {
ptr::write_bytes((value as *mut T).cast::<u8>(), 0,
std::mem::size_of_val(value))
}
}
? And it works for both sized values and slices. If Zeroable is
limited to sized types, I guess you could still do:
pub fn write_zero_slice<T: Zeroable>(value: &mut [T]) {
ptr::write_bytes(value.as_mut_ptr(), 0, value.len())
}
So the question is whether the ZERO constant is worthwhile enough, to
justify the limitation of the Sized bound (e.g. having separate
write_zero and write_zero_slice in the future).
Thanks,
Paolo
Powered by blists - more mailing lists