From 7d040b7d13e6fb6e143f1c14987bcc2f48dc585b Mon Sep 17 00:00:00 2001 From: Avi Weinstock Date: Tue, 19 Nov 2019 18:01:24 -0500 Subject: [PATCH] Add UninitScalar newtype for defining traits on a MaybeUninit. --- src/base/allocator.rs | 3 ++- src/base/scalar.rs | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/base/allocator.rs b/src/base/allocator.rs index 246f3620e..71db540ae 100644 --- a/src/base/allocator.rs +++ b/src/base/allocator.rs @@ -5,7 +5,7 @@ use std::any::Any; use crate::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; use crate::base::dimension::{Dim, U1}; use crate::base::storage::ContiguousStorageMut; -use crate::base::{DefaultAllocator, Scalar}; +use crate::base::{DefaultAllocator, Scalar, UninitScalar}; /// A matrix allocator of a memory buffer that may contain `R::to_usize() * C::to_usize()` /// elements of type `N`. @@ -21,6 +21,7 @@ pub trait Allocator: Any + Sized { type Buffer: ContiguousStorageMut + Clone; /// Allocates a buffer with the given number of rows and columns without initializing its content. + //unsafe fn allocate_uninitialized(nrows: R, ncols: C) -> , R, C>>::Buffer; unsafe fn allocate_uninitialized(nrows: R, ncols: C) -> Self::Buffer; /// Allocates a buffer initialized with the content of the given iterator. diff --git a/src/base/scalar.rs b/src/base/scalar.rs index a6c837ffa..e9750825a 100644 --- a/src/base/scalar.rs +++ b/src/base/scalar.rs @@ -1,6 +1,7 @@ use std::any::Any; use std::any::TypeId; -use std::fmt::Debug; +use std::fmt::{self, Debug, Formatter}; +use std::mem::MaybeUninit; /// The basic scalar type for all structures of `nalgebra`. /// @@ -15,3 +16,25 @@ pub trait Scalar: PartialEq + Debug + Any { } } impl Scalar for T {} + +/// A newtype wrapper that provides the necessary trait implementations for MaybeUninit +/// This is needed because MaybeUninit does not pass through implementations on uninitialized data (for good reason) +pub struct UninitScalar(MaybeUninit); + +impl Clone for UninitScalar { + fn clone(&self) -> Self { + panic!("Clone::clone called on potentially uninitialized data") + } +} + +impl PartialEq for UninitScalar { + fn eq(&self, _: &Self) -> bool { + panic!("PartialEq::eq called on potentially uninitalized data") + } +} +impl Debug for UninitScalar { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "UninitScalar") + } +} +impl Scalar for UninitScalar {}