用法
pub fn extend(&self, next:Layout) -> Result<(Layout, usize), LayoutError>
创建一个布局,说明 self
后跟 next
的记录,包括任何必要的填充以确保 next
正确对齐,但没有尾随填充。
为了匹配 C 表示布局 repr(C)
,您应该在使用所有字段扩展布局后调用 pad_to_align
。 (没有办法匹配默认的 Rust 表示布局 repr(Rust)
,因为它是未指定的。)
请注意,生成的布局的对齐方式将是 self
和 next
的最大对齐方式,以确保两个部分的对齐。
返回 Ok((k, offset))
,其中 k
是连接记录的布局,而 offset
是嵌入在连接记录中的 next
开始的相对位置(以字节为单位)(假设记录本身从偏移量 0 开始)。
算术溢出时,返回 LayoutError
。
例子
要计算 #[repr(C)]
结构的布局以及字段与其字段布局的偏移量:
pub fn repr_c(fields:&[Layout]) -> Result<(Layout, Vec<usize>), LayoutError> {
let mut offsets = Vec::new();
let mut layout = Layout::from_size_align(0, 1)?;
for &field in fields {
let (new_layout, offset) = layout.extend(field)?;
layout = new_layout;
offsets.push(offset);
}
// Remember to finalize with `pad_to_align`!
Ok((layout.pad_to_align(), offsets))
}
相关用法
- Rust std::alloc::GlobalAlloc用法及代码示例
- Rust std::alloc::alloc用法及代码示例
- Rust std::alloc::System用法及代码示例
- Rust std::alloc::alloc_zeroed用法及代码示例
- Rust std::array::try_from_fn用法及代码示例
- Rust std::any::Any.downcast_mut用法及代码示例
- Rust std::any::Any.is用法及代码示例
- Rust std::assert用法及代码示例
- Rust std::assert_eq用法及代码示例
- Rust std::any::type_name用法及代码示例
- Rust std::any::type_name_of_val用法及代码示例
- Rust std::ascii::AsciiExt用法及代码示例
- Rust std::any::Any.downcast_ref用法及代码示例
- Rust std::array::IntoIter.new用法及代码示例
- Rust std::any::Any.type_id用法及代码示例
- Rust std::assert_matches::debug_assert_matches用法及代码示例
- Rust std::ascii::escape_default用法及代码示例
- Rust std::assert_matches::assert_matches用法及代码示例
- Rust std::assert_ne用法及代码示例
- Rust std::any::TypeId.of用法及代码示例
注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 std::alloc::Layout.extend。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。