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


/// A trait to unify ArrayVec and Vec when dealing with NonNull pointers.
pub trait NonNullUnifiedArrayVecAndVec<T> : UnifiedArrayVecAndVec<NonNull<T>>
{
	/// To FFI data.
	///
	/// Returns a pointer and a length for FFI functions to read from.
	#[inline(always)]
	fn to_ffi_data(&mut self, start_from_index: usize) -> (*mut *mut T, usize)
	{
		let length = self.length();
		debug_assert!(length >= start_from_index, "length '{}' is less than start_from_index '{}'", length, start_from_index);
		
		let number_of_potential_packets = length - start_from_index;
		debug_assert_ne!(number_of_potential_packets, 0, "number_of_potential_packets is zero");
		
		let pointer: *mut *mut T = unsafe { transmute(self.mutable_pointer_at_length(start_from_index) as *mut NonNull<T>) };
		
		(pointer, number_of_potential_packets)
	}
	
	/// To FFI data.
	///
	/// Returns a pointer and a length (u16) for FFI functions to read from.
	#[inline(always)]
	fn to_ffi_data_u16(&mut self, start_from_index: usize) -> (*mut *mut T, u16)
	{
		let (pointer, number_of_potential_packets) = self.to_ffi_data(start_from_index);
		debug_assert!(number_of_potential_packets <= ::std::u16::MAX as usize, "number_of_potential_packets '{}' exceeds ::std::u16::MAX '{}'", number_of_potential_packets, ::std::u16::MAX);
		(pointer, number_of_potential_packets as u16)
	}
	
	/// To FFI data.
	///
	/// Returns a pointer and a length (u32) for FFI functions to read from.
	#[inline(always)]
	fn to_ffi_data_u32(&mut self, start_from_index: usize) -> (*mut *mut T, u32)
	{
		let (pointer, number_of_potential_packets) = self.to_ffi_data(start_from_index);
		debug_assert!(number_of_potential_packets <= ::std::u32::MAX as usize, "number_of_potential_packets '{}' exceeds ::std::u32::MAX '{}'", number_of_potential_packets, ::std::u32::MAX);
		(pointer, number_of_potential_packets as u32)
	}
	
	/// From FFI data.
	///
	/// Returns a pointer and a length for FFI functions to write to.
	#[inline(always)]
	fn from_ffi_data(&mut self) -> (*mut *mut T, usize)
	{
		let length = self.length();
		let maximum_capacity = self.maximum_capacity();
		debug_assert!(maximum_capacity >= length, "maximum_capacity '{}' is less than length '{}'", maximum_capacity, length);
		
		let number_of_potential_packets = maximum_capacity - length;
		debug_assert_ne!(number_of_potential_packets, 0, "number_of_potential_packets is zero");
		
		let pointer: *mut *mut T = unsafe { transmute(self.mutable_pointer_at_length(length) as *mut NonNull<T>) };
		
		(pointer, number_of_potential_packets)
	}
	
	/// From FFI data.
	///
	/// Returns a pointer and a length (u16) for FFI functions to write to.
	#[inline(always)]
	fn from_ffi_data_u16(&mut self) -> (*mut *mut T, u16)
	{
		let (pointer, number_of_potential_packets) = self.from_ffi_data();
		debug_assert!(number_of_potential_packets <= ::std::u16::MAX as usize, "number_of_potential_packets '{}' exceeds ::std::u16::MAX '{}'", number_of_potential_packets, ::std::u16::MAX);
		(pointer, number_of_potential_packets as u16)
	}
	
	/// From FFI data.
	///
	/// Returns a pointer and a length (u32) for FFI functions to write to.
	#[inline(always)]
	fn from_ffi_data_u32(&mut self) -> (*mut *mut T, u32)
	{
		let (pointer, number_of_potential_packets) = self.from_ffi_data();
		debug_assert!(number_of_potential_packets <= ::std::u32::MAX as usize, "number_of_potential_packets '{}' exceeds ::std::u32::MAX '{}'", number_of_potential_packets, ::std::u32::MAX);
		(pointer, number_of_potential_packets as u32)
	}
}

impl<T> NonNullUnifiedArrayVecAndVec<T> for Vec<NonNull<T>>
{
}