[Media] I wish there was a simpler way [the most cursed code i ever written]

148 points FKwilczek 24 comments
[Media] I wish there was a simpler way [the most cursed code i ever written]

The most cursed idea i ever have. Don't use this in prod probably have a lot of ub. Enjoy.

use std::{
	mem::transmute,
	ops::{Index, IndexMut, Range},
	slice::{from_raw_parts, from_raw_parts_mut},
};

fn main() {
	let mut arr = Arr2d {
		width: 2,
		data: vec![1, 2, 3, 4, 5, 6],
	};

	let slice = &mut arr[1..3];
	let row = &mut slice[0];
	row[1] = 12;
	println!("{arr:?}");
}

#[repr(transparent)]
struct Rows<T>([T]);

impl<T> Rows<T> {
	fn new_ref_mut(data: &mut [T], width: usize) -> &mut Rows<T> {
		let ptr = Box::leak(Box::new(Data {
			size: data.len(),
			width,
		}));

		unsafe {
			transmute::<&mut [T], &mut Rows<T>>(from_raw_parts_mut(
				data.as_mut_ptr(),
				ptr as *mut Data as usize,
			))
		}
	}

	fn size(&self) -> usize {
		unsafe {
			(self.0.len() as *const Data)
				.as_ref()
				.unwrap_unchecked()
				.size
		}
	}

	fn as_ref(&self) -> &[T] {
		let size = self.size();
		unsafe { from_raw_parts(self.0.as_ptr(), size) }
	}

	fn as_mut(&mut self) -> &mut [T] {
		let size = self.size();
		unsafe { from_raw_parts_mut(self.0.as_mut_ptr(), size) }
	}

	fn new_ref(data: &[T], width: usize) -> &Rows<T> {
		let ptr = Box::leak(Box::new(Data {
			size: data.len(),
			width,
		}));

		unsafe {
			transmute::<&[T], &Rows<T>>(from_raw_parts(data.as_ptr(), ptr as *mut Data as usize))
		}
	}

	fn width(&self) -> usize {
		unsafe {
			(self.0.len() as *const Data)
				.as_ref()
				.unwrap_unchecked()
				.width
		}
	}
}

struct Data {
	size: usize,
	width: usize,
}

#[derive(Debug)]
struct Arr2d<T> {
	width: usize,
	data: Vec<T>,
}

impl<T> Index<Range<usize>> for Rows<T> {
	type Output = Rows<T>;
	fn index(&self, index: Range<usize>) -> &Self::Output {
		let width = self.width();
		let array = self.as_ref();
		Rows::new_ref(&array[index.start * width..index.end * width], width)
	}
}

impl<T> IndexMut<Range<usize>> for Rows<T> {
	fn index_mut(&mut self, index: Range<usize>) -> &mut Self::Output {
		let width = self.width();
		let array = self.as_mut();
		Rows::new_ref_mut(&mut array[index.start * width..index.end * width], width)
	}
}

impl<T> Index<usize> for Rows<T> {
	type Output = [T];
	fn index(&self, index: usize) -> &[T] {
		let width = self.width();
		let array = self.as_ref();
		&array[index * width..(index + 1) * width]
	}
}

impl<T> IndexMut<usize> for Rows<T> {
	fn index_mut(&mut self, index: usize) -> &mut [T] {
		let width = self.width();
		let array = self.as_mut();
		&mut array[index * width..(index + 1) * width]
	}
}

impl<T> Index<Range<usize>> for Arr2d<T> {
	type Output = Rows<T>;
	fn index(&self, index: Range<usize>) -> &Self::Output {
		Rows::new_ref(
			&self.data[index.start * self.width..index.end * self.width],
			self.width,
		)
	}
}

impl<T> IndexMut<Range<usize>> for Arr2d<T> {
	fn index_mut(&mut self, index: Range<usize>) -> &mut Self::Output {
		Rows::new_ref_mut(
			&mut self.data[index.start * self.width..index.end * self.width],
			self.width,
		)
	}
}