devela/data/dst/value.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
//! Implementation of the single-value structure
//
// TOC
// - public API
// - private API
// - core_impls
use super::{check_fat_pointer, decompose_pointer, store_metadata, DstArray, DstBuf};
#[cfg(doc)]
use crate::MaybeUninit;
use crate::{
ManuallyDrop, Mem, MemAligned,
_core::{marker, ptr},
};
/* public API */
/// A statically allocated <abbr title="Dynamically sized type">DST</abbr>
/// value with pointer alignment.
///
/// # Examples
/// ```
/// # use devela::data::DstValueUsize;
/// let v = DstValueUsize::<[u8], 16>::new([1,2,3], |v| v);
/// ```
// WAIT: [lazy_type_alias](https://github.com/rust-lang/rust/issues/112792) ↓DENIED
pub type DstValueUsize<DST /*: ?Sized*/, const CAP: usize> = DstValue<DST, DstArray<usize, CAP>>;
/// A statically allocated <abbr title="Dynamically sized type">DST</abbr> value.
///
/// `DST` is the unsized type contained.
/// `BUF` is the buffer used to hold the unsized type (both data and metadata).
///
/// # Examples
/// ```
/// # use {devela::data::{DstArray, DstValue}, core::fmt::Display};
/// let val = DstValue::<dyn Display, DstArray<usize, 2>>::new(123456, |v| v as _)
/// .expect("Insufficient size");
/// assert_eq!( format!("{}", val), "123456" );
/// ```
pub struct DstValue<DST: ?Sized, BUF: DstBuf> {
_pd: marker::PhantomData<DST>,
// Data contains the object data first, then padding, then the pointer information
data: BUF,
}
impl<DST: ?Sized, BUF: DstBuf> DstValue<DST, BUF> {
/// Construct a stack-based DST.
///
/// The closure `get_ref` must just convert `&VAL` to `&VAL`
/// (if the pointers don't match, an assertion triggers).
///
/// Returns `Ok(dst)` if the allocation was successful, or `Err(val)` if it failed.
///
/// # Examples
/// ```
/// # use {devela::data::{DstArray, DstValue}, core::fmt::Display};
/// let val = DstValue::<dyn Display, DstArray<usize, 2>>::new(1234, |v| v as _)
/// .expect("Insufficient size");
/// assert_eq!( format!("{}", val), "1234" );
/// ```
pub fn new<VAL, F>(val: VAL, get_ref: F) -> Result<DstValue<DST, BUF>, VAL>
where
F: FnOnce(&VAL) -> &DST,
(VAL, BUF::Inner): MemAligned,
BUF: Default,
{
Self::in_buffer(BUF::default(), val, get_ref)
}
/// Construct a stack-based DST using the given `buffer`.
///
/// See `new` for requirements on the `get_ref` closure.
///
/// Returns `Ok(dst)` if the allocation was successful, or `Err(val)` if it failed.
///
/// # Examples
/// ```
/// # use {devela::data::DstValue, core::{fmt::Display, mem::MaybeUninit}};
/// let val = DstValue::<dyn Display, _>::in_buffer([MaybeUninit::new(0u64); 2], 1234, |v| v)
/// .expect("Insufficient size");
/// assert_eq!( format!("{}", val), "1234" );
/// ```
pub fn in_buffer<VAL, F: FnOnce(&VAL) -> &DST>(
buffer: BUF,
val: VAL,
get_ref: F,
) -> Result<DstValue<DST, BUF>, VAL>
where
(VAL, BUF::Inner): MemAligned,
{
<(VAL, BUF::Inner) as MemAligned>::assert_compatibility();
let rv = unsafe {
let ptr: *const _ = check_fat_pointer(&val, get_ref);
let (raw_ptr, meta_len, meta) = decompose_pointer(ptr);
DstValue::new_raw(&meta[..meta_len], raw_ptr.cast_mut(), size_of::<VAL>(), buffer)
};
match rv {
Some(r) => {
// Prevent the destructor from running, now that we've copied it away
Mem::forget(val);
Ok(r)
}
None => Err(val),
}
}
/// Returns a new raw value.
///
/// # Safety
/// `data` must point to `size` bytes, which shouldn't be freed if `Some` is returned.
pub unsafe fn new_raw(
info: &[usize],
data: *mut (),
size: usize,
mut buffer: BUF,
) -> Option<DstValue<DST, BUF>> {
let req_words = BUF::round_to_words(size_of_val(info)) + BUF::round_to_words(size);
if buffer.extend(req_words).is_err() {
return None;
}
let mut rv =
ManuallyDrop::new(DstValue::<DST, BUF> { _pd: marker::PhantomData, data: buffer });
// SAFETY: caller must ensure safety
unsafe {
rv.write_value(data, size, info);
}
Some(ManuallyDrop::into_inner(rv))
}
/// Replace the contents without dropping the backing allocation
///
/// # Examples
/// ```
/// # use {devela::data::{DstArray, DstValue}, core::fmt::Display};
/// let mut value = DstValue::<dyn Display, DstArray<usize, 2>>::new(1234, |v| v)
/// .unwrap();
/// assert_eq!(format!("{}", value), "1234");
/// value.replace(1.234, |v| v).unwrap();
/// assert_eq!(format!("{}", value), "1.234");
/// ```
pub fn replace<VAL>(&mut self, val: VAL, get_ref: impl Fn(&VAL) -> &DST) -> Result<(), VAL>
where
(VAL, BUF::Inner): MemAligned,
{
<(VAL, BUF::Inner) as MemAligned>::assert_compatibility();
let size = size_of::<VAL>();
let (raw_ptr, meta_len, meta) = decompose_pointer(check_fat_pointer(&val, get_ref));
let info = &meta[..meta_len];
// Check size requirements (allow resizing)
let req_words = BUF::round_to_words(size_of_val(info)) + BUF::round_to_words(size);
if self.data.extend(req_words).is_err() {
return Err(val);
}
// If met, drop the existing item and move in the new item
unsafe {
ptr::drop_in_place::<DST>(&mut **self);
self.write_value(raw_ptr, size_of::<VAL>(), info);
}
Ok(())
}
}
/// # Specialisations for `str` (allowing storage of strings with single-byte alignment)
impl<BUF: DstBuf> DstValue<str, BUF> {
/// Create a new empty string with a default buffer
pub fn empty_str() -> Result<Self, ()>
where
BUF: Default,
{
Self::empty_str_in_buffer(Default::default())
}
/// Create a new empty string with a provided buffer
pub fn empty_str_in_buffer(buffer: BUF) -> Result<Self, ()> {
let rv = unsafe {
let (raw_ptr, meta_len, meta) = decompose_pointer("");
DstValue::new_raw(&meta[..meta_len], raw_ptr.cast_mut(), 0, buffer)
};
match rv {
Some(r) => Ok(r),
None => Err(()),
}
}
/// Construct from a `str` using a default-constructed buffer
/// # Examples
/// ```
/// # use {devela::data::{DstArray, DstValue}, core::fmt::Display};
/// let val = DstValue::<str, DstArray<u8, 32>>::new_str("Hello, World")
/// .expect("Insufficient size");
/// assert_eq!( &val[..], "Hello, World" );
/// ```
pub fn new_str(v: &str) -> Result<Self, &str>
where
BUF: Default,
{
Self::new_str_in_buffer(Default::default(), v)
}
/// Construct from a `str` using a provided buffer
///
/// # Examples
/// ```
/// # use {devela::data::DstValue, core::{fmt::Display, mem::MaybeUninit}};
/// let val = DstValue::new_str_in_buffer([MaybeUninit::new(0u8); 32], "Hello, World")
/// .expect("Insufficient size");
/// assert_eq!( &val[..], "Hello, World" );
/// ```
pub fn new_str_in_buffer(buffer: BUF, val: &str) -> Result<Self, &str> {
let rv = unsafe {
let (raw_ptr, meta_len, meta) = decompose_pointer(val);
DstValue::new_raw(&meta[..meta_len], raw_ptr.cast_mut(), size_of_val(val), buffer)
};
match rv {
Some(r) => Ok(r),
None => Err(val),
}
}
/// Add a string to the end of a string
///
/// # Examples
/// ```
/// # use devela::data::{DstArray, DstValue};
/// let mut s = DstValue::<str, DstArray<usize, 8>>::new_str("Foo").unwrap();
/// s.append_str("Bar").unwrap();
/// assert_eq!(&s[..], "FooBar");
/// ```
pub fn append_str(&mut self, val: &str) -> Result<(), ()> {
let info_words = BUF::round_to_words(size_of::<usize>());
let ofs = self.len();
// Check/expand sufficient space
let req_words = BUF::round_to_words(ofs + val.len()) + info_words;
if self.data.extend(req_words).is_err() {
return Err(());
}
// Get the metadata slot
let data = self.data.as_mut();
let info_ofs = data.len() - info_words;
unsafe {
ptr::copy_nonoverlapping(
val.as_ptr(),
(data.as_mut_ptr() as *mut u8).add(ofs),
val.len(),
);
store_metadata(&mut data[info_ofs..], &[ofs + val.len()]);
}
Ok(())
}
/// Resize the string (discarding trailing data)
///
/// # Examples
/// ```
/// # use devela::data::{DstArray, DstValue};
/// let mut s = DstValue::<str, DstArray<usize, 8>>::new_str("FooBar").unwrap();
/// s.truncate(3);
/// assert_eq!(&s[..], "Foo");
/// ```
pub fn truncate(&mut self, len: usize) {
if len < self.len() {
let _ = &self[..][len..]; // Index to force a panic if the index isn't char-aligned
let info_words = BUF::round_to_words(size_of::<usize>());
let data = self.data.as_mut();
let info_ofs = data.len() - info_words;
store_metadata(&mut data[info_ofs..], &[len]);
}
}
}
/// # Specialisation for slices (acting like an `ArrayVec`)
impl<I, BUF: DstBuf> DstValue<[I], BUF>
where
(I, BUF::Inner): MemAligned,
{
/// Create a new zero-sized slice.
///
/// # Errors
/// Will error only if the metadata doesn't fit.
pub fn empty_slice() -> Result<Self, ()>
where
BUF: Default,
{
Self::empty_slice_with_buffer(Default::default())
}
/// Create a new zero-sized slice in the provided buffer.
///
/// # Errors
/// Will error only if the metadata doesn't fit.
pub fn empty_slice_with_buffer(mut buffer: BUF) -> Result<Self, ()> {
<(I, BUF::Inner) as MemAligned>::assert_compatibility();
let info_words = BUF::round_to_words(size_of::<usize>());
let req_words = info_words;
if buffer.extend(req_words).is_err() {
return Err(());
}
assert!(req_words <= buffer.as_ref().len());
let mut rv = DstValue { _pd: marker::PhantomData, data: buffer };
let data = rv.data.as_mut();
let info_ofs = data.len() - info_words;
let (_data_dst, info_dst) = data.split_at_mut(info_ofs);
store_metadata(info_dst, &[0]);
Ok(rv)
}
/// Append an item to the end of the slice (similar to `Vec::push`)
pub fn append(&mut self, v: I) -> Result<(), I> {
let info_words = BUF::round_to_words(size_of::<usize>());
let ofs = self.len();
// Check/expand sufficient space
let req_words = BUF::round_to_words((ofs + 1) * size_of::<I>()) + info_words;
if self.data.extend(req_words).is_err() {
return Err(v);
}
let data = self.data.as_mut();
assert!(req_words <= data.len());
// Write the new value
// SAFETY: Alignment is checked, pointer is in-bounds.
unsafe {
let data_ptr = (data.as_ptr() as *mut I).add(ofs);
ptr::write(data_ptr, v);
}
// Only update item count after the write
let info_ofs = data.len() - info_words;
store_metadata(&mut data[info_ofs..], &[ofs + 1]);
Ok(())
}
/// Inline append an item (See [`Self::append`]).
pub fn appended(mut self, v: I) -> Result<Self, (Self, I)> {
match self.append(v) {
Ok(()) => Ok(self),
Err(v) => Err((self, v)),
}
}
/// Extend a slice with an iterator.
pub fn extend<It: Iterator<Item = I>>(&mut self, mut iter: It) -> Result<(), (I, It)> {
while let Some(v) = iter.next() {
match self.append(v) {
Ok(()) => {}
Err(v) => return Err((v, iter)),
}
}
Ok(())
}
/// Helper to extend during construction (See [`Self::extend`]).
pub fn extended<It: Iterator<Item = I>>(mut self, iter: It) -> Result<Self, (Self, I, It)> {
match self.extend(iter) {
Ok(()) => Ok(self),
Err((v, iter)) => Err((self, v, iter)),
}
}
/// Remove the last item from the slice.
pub fn pop(&mut self) -> Option<I> {
if self.len() > 0 {
let ofs = self.len() - 1;
let data = self.data.as_mut();
let info_words = BUF::round_to_words(size_of::<usize>());
let info_ofs = data.len() - info_words;
unsafe {
store_metadata(&mut data[info_ofs..], &[ofs]);
Some(ptr::read((data.as_ptr() as *const I).add(ofs)))
}
} else {
None
}
}
}
/* private API */
impl<DST: ?Sized, BUF: DstBuf> DstValue<DST, BUF> {
unsafe fn write_value(&mut self, data: *const (), size: usize, info: &[usize]) {
let info_words = BUF::round_to_words(size_of_val(info));
let req_words = info_words + BUF::round_to_words(size);
let buf = self.data.as_mut();
assert!(req_words <= buf.len());
// Place pointer information at the end of the region
// - Allows the data to be at the start for alignment purposes
{
let info_ofs = buf.len() - info_words;
let info_dst = &mut buf[info_ofs..];
store_metadata(info_dst, info);
}
// SAFETY: caller must ensure safety
unsafe {
ptr::copy_nonoverlapping(data as *const u8, buf.as_mut_ptr() as *mut u8, size);
}
}
// Obtain raw pointer to the contained data
unsafe fn as_ptr(&self) -> *mut DST {
let data = self.data.as_ref();
let info_size = size_of::<*mut DST>() / size_of::<usize>() - 1;
let info_ofs = data.len() - BUF::round_to_words(info_size * size_of::<usize>());
let (data, meta) = data.split_at(info_ofs);
// SAFETY: caller must ensure safety
unsafe { super::make_fat_ptr(data.as_ptr() as *mut (), meta) }
}
// Obtain raw pointer to the contained data
unsafe fn as_ptr_mut(&mut self) -> *mut DST {
let data = self.data.as_mut();
let info_size = size_of::<*mut DST>() / size_of::<usize>() - 1;
let info_ofs = data.len() - BUF::round_to_words(info_size * size_of::<usize>());
let (data, meta) = data.split_at_mut(info_ofs);
// SAFETY: caller must ensure safety
unsafe { super::make_fat_ptr(data.as_mut_ptr() as *mut (), meta) }
}
}
mod core_impls {
use super::{DstBuf, DstValue};
use core::{fmt, future, iter, ops, pin, ptr, task};
impl<DST: ?Sized, BUF: DstBuf> ops::Deref for DstValue<DST, BUF> {
type Target = DST;
#[must_use]
fn deref(&self) -> &DST {
unsafe { &*self.as_ptr() }
}
}
impl<DST: ?Sized, BUF: DstBuf> ops::DerefMut for DstValue<DST, BUF> {
#[must_use]
fn deref_mut(&mut self) -> &mut DST {
unsafe { &mut *self.as_ptr_mut() }
}
}
impl<DST: ?Sized, BUF: DstBuf> ops::Drop for DstValue<DST, BUF> {
fn drop(&mut self) {
unsafe { ptr::drop_in_place(&mut **self) }
}
}
macro_rules! impl_trait {
( $t:path; $($body:tt)* ) => {
impl<BUF: DstBuf, DST: ?Sized> $t for DstValue<DST, BUF> where DST: $t { $( $body )* }
}
}
impl_trait! { future::Future;
type Output = DST::Output;
fn poll(self: pin::Pin<&mut Self>, cx: &mut task::Context) -> task::Poll<Self::Output> {
unsafe { pin::Pin::new_unchecked(&mut **self.get_unchecked_mut()).poll(cx) }
}
}
impl_trait! { iter::Iterator;
type Item = DST::Item;
#[must_use]
fn next(&mut self) -> Option<Self::Item> {
(**self).next()
}
}
impl_trait! { iter::DoubleEndedIterator;
#[must_use]
fn next_back(&mut self) -> Option<Self::Item> {
(**self).next_back()
}
}
impl_trait! { iter::ExactSizeIterator; }
macro_rules! impl_fmt {
( $( $t:ident )* ) => {
$(
impl_trait!{ fmt::$t;
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(**self).fmt(f)
}
}
)*
}
}
impl_fmt! {
Display Debug UpperHex LowerHex
}
}