MCPcopy Create free account
hub / github.com/apache/arrow-rs / from_len_zeroed

Method from_len_zeroed

arrow-buffer/src/buffer/mutable.rs:167–184  ·  view source on GitHub ↗

Allocates a new [MutableBuffer] with `len` and capacity to be at least `len` where all bytes are guaranteed to be `0u8`. # Example ``` # use arrow_buffer::buffer::{Buffer, MutableBuffer}; let mut buffer = MutableBuffer::from_len_zeroed(127); assert_eq!(buffer.len(), 127); assert!(buffer.capacity() >= 127); let data = buffer.as_slice_mut(); assert_eq!(data[126], 0u8); ``` # Panics Panics if `len`

(len: usize)

Source from the content-addressed store, hash-verified

165 ///
166 /// Panics if `len` is too large to construct a valid allocation [`Layout`]
167 pub fn from_len_zeroed(len: usize) -> Self {
168 let layout = Layout::from_size_align(len, ALIGNMENT).unwrap();
169 let data = match layout.size() {
170 0 => dangling_ptr(),
171 _ => {
172 // Safety: Verified size != 0
173 let raw_ptr = unsafe { std::alloc::alloc_zeroed(layout) };
174 NonNull::new(raw_ptr).unwrap_or_else(|| handle_alloc_error(layout))
175 }
176 };
177 Self {
178 data,
179 len,
180 layout,
181 #[cfg(feature = "pool")]
182 reservation: std::sync::Mutex::new(None),
183 }
184 }
185
186 /// Allocates a new [MutableBuffer] from given `Bytes`.
187 pub(crate) fn from_bytes(bytes: Bytes) -> Result<Self, Bytes> {

Callers

nothing calls this directly

Calls 2

dangling_ptrFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected