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
// This file is part of context-coroutine. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/context-coroutine/master/COPYRIGHT. No part of context-coroutine, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2019 The developers of context-coroutine. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/context-coroutine/master/COPYRIGHT.


/// Data that can be transfered to a continuation.
pub trait TransferableData
{
	/// Into usize; panics on failure.
	fn into_usize(self) -> usize;

	/// From usize; panics on failure.
	fn from_usize(value: usize) -> Self;
}

macro_rules! as_transferable_data
{
	($name: tt) =>
	{
		impl TransferableData for $name
		{
			#[inline(always)]
			fn into_usize(self) -> usize
			{
				self as usize
			}

			#[inline(always)]
			fn from_usize(value: usize) -> Self
			{
				value as $name
			}
		}
	}
}

as_transferable_data!(u8);
as_transferable_data!(u16);
as_transferable_data!(u32);
#[cfg(target_pointer_width = "64")] as_transferable_data!(u64);
as_transferable_data!(usize);

as_transferable_data!(i8);
as_transferable_data!(i16);
as_transferable_data!(i32);
#[cfg(target_pointer_width = "64")] as_transferable_data!(i64);
as_transferable_data!(isize);

as_transferable_data!(f32);
#[cfg(target_pointer_width = "64")] as_transferable_data!(f64);

impl TransferableData for ()
{
	#[inline(always)]
	fn into_usize(self) -> usize
	{
		0
	}

	#[inline(always)]
	fn from_usize(value: usize) -> Self
	{
		debug_assert!(value == 0, "value was not zero");

		()
	}
}

impl TransferableData for bool
{
	#[inline(always)]
	fn into_usize(self) -> usize
	{
		self as usize
	}

	#[inline(always)]
	fn from_usize(value: usize) -> Self
	{
		value != 0
	}
}

impl<T> TransferableData for *const T
{
	#[inline(always)]
	fn into_usize(self) -> usize
	{
		self as usize
	}

	#[inline(always)]
	fn from_usize(value: usize) -> Self
	{
		value as *const T
	}
}

impl<T> TransferableData for *mut T
{
	#[inline(always)]
	fn into_usize(self) -> usize
	{
		self as usize
	}

	#[inline(always)]
	fn from_usize(value: usize) -> Self
	{
		value as *mut T
	}
}

impl<T> TransferableData for NonNull<T>
{
	#[inline(always)]
	fn into_usize(self) -> usize
	{
		self.as_ptr() as usize
	}

	#[inline(always)]
	fn from_usize(value: usize) -> Self
	{
		let pointer = value as *mut T;
		debug_assert!(!pointer.is_null(), "pointer value is null");
		unsafe { NonNull::new_unchecked(pointer) }
	}
}