rust/pin-init/internal/src/zeroable.rs
Source file repositories/reference/linux-study-clean/rust/pin-init/internal/src/zeroable.rs
File Facts
- System
- Linux kernel
- Corpus path
rust/pin-init/internal/src/zeroable.rs- Extension
.rs- Size
- 2792 bytes
- Lines
- 79
- Domain
- Rust Kernel Layer
- Bucket
- Rust API Membrane
- Inferred role
- Rust Kernel Layer: implementation source
- Status
- source implementation candidate
Why This File Exists
Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.
- Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
function Ok
Annotated Snippet
// SPDX-License-Identifier: Apache-2.0 OR MIT
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Data, DeriveInput, Field, Fields};
use crate::{diagnostics::ErrorGuaranteed, DiagCtxt};
pub(crate) fn derive(
input: DeriveInput,
dcx: &mut DiagCtxt,
) -> Result<TokenStream, ErrorGuaranteed> {
let fields = match input.data {
Data::Struct(data_struct) => data_struct.fields,
Data::Union(data_union) => Fields::Named(data_union.fields),
Data::Enum(data_enum) => {
return Err(dcx.error(data_enum.enum_token, "cannot derive `Zeroable` for an enum"));
}
};
let name = input.ident;
let mut generics = input.generics;
for param in generics.type_params_mut() {
param.bounds.insert(0, parse_quote!(::pin_init::Zeroable));
}
let (impl_gen, ty_gen, whr) = generics.split_for_impl();
let field_type = fields.iter().map(|field| &field.ty);
Ok(quote! {
// SAFETY: Every field type implements `Zeroable` and padding bytes may be zero.
#[automatically_derived]
unsafe impl #impl_gen ::pin_init::Zeroable for #name #ty_gen
#whr
{}
const _: () = {
fn assert_zeroable<T: ?::core::marker::Sized + ::pin_init::Zeroable>() {}
fn ensure_zeroable #impl_gen ()
#whr
{
#(
assert_zeroable::<#field_type>();
)*
}
};
})
}
pub(crate) fn maybe_derive(
input: DeriveInput,
dcx: &mut DiagCtxt,
) -> Result<TokenStream, ErrorGuaranteed> {
let fields = match input.data {
Data::Struct(data_struct) => data_struct.fields,
Data::Union(data_union) => Fields::Named(data_union.fields),
Data::Enum(data_enum) => {
return Err(dcx.error(data_enum.enum_token, "cannot derive `Zeroable` for an enum"));
}
};
let name = input.ident;
let mut generics = input.generics;
for param in generics.type_params_mut() {
param.bounds.insert(0, parse_quote!(::pin_init::Zeroable));
}
for Field { ty, .. } in fields {
generics
.make_where_clause()
.predicates
// the `for<'__dummy>` HRTB makes this not error without the `trivial_bounds`
// feature <https://github.com/rust-lang/rust/issues/48214#issuecomment-2557829956>.
.push(parse_quote!(#ty: for<'__dummy> ::pin_init::Zeroable));
}
let (impl_gen, ty_gen, whr) = generics.split_for_impl();
Ok(quote! {
// SAFETY: Every field type implements `Zeroable` and padding bytes may be zero.
#[automatically_derived]
unsafe impl #impl_gen ::pin_init::Zeroable for #name #ty_gen
#whr
{}
})
}
Annotation
- Detected declarations: `function Ok`.
- Atlas domain: Rust Kernel Layer / Rust API Membrane.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.