« first day (3342 days earlier)   

1:30 PM
Hi! I'm trying to write a function that turns `[ Option<T>; N ]` into `Option< [ T; N ] >`, only if every item in the array is `Some`.
This is the first time I'm using `unsafe`, can you tell me if I did it correctly?
use std::mem::MaybeUninit;

pub trait RewrapArray
{
	type Output;

	fn rewrap(self) -> Self::Output;
}

impl< T, const N: usize > RewrapArray for [ Option<T>; N ]
{
	type Output = Option< [ T; N ] >;

	fn rewrap(self) -> Self::Output
	{
		let mut output: [ MaybeUninit<T>; N ] = unsafe { MaybeUninit::uninit().assume_init() };

		for ( i, input_item ) in self.into_iter().enumerate()
		{
			match input_item
			{
				Some(item) =>
				{
					output[i].write(item);
				}

				None =>
				{
					for output_item in &mut output[ 0 .. i ]
(Also, is there a way in the standard library to do this without allocation?)
 
1:49 PM
Apart from going against all Rust style guides, it seems sound, there is try_map on arrays which you can use like array_of_options.try_map(std::convert::identity), but it's not stable yet.
 
Sound means all the unsafe stuff is used safely?
 
 
5 hours later…
7:01 PM
while following crafting interpreter's book, I'm tempted to "bubble up" errors from results, so that I can accumulate them all and probably display them at the end. I naively thought I'd create various error types, for various stages (scanning, parsing) but I feel like the compiler will yell at me if I try to put them all in a single vector. Which would probably probe me towards doing something unfortunate like impl Error stuff, idk.
1. Is stacking errors a pattern, is there a term that design it I could search for?
2. Is it simply better to use a single Error type and maybe pass in a Enum to differentiate the errors?
 

« first day (3342 days earlier)