cobia/
cape_array_enumeration.rs

1use std::fmt;
2use crate::cape_array::CapeArrayRefIterator;
3use crate::{COBIAError,CapeArrayEnumerationProviderIn,CapeArrayEnumerationProviderOut};
4use crate::C;
5use crate::cape_result_value::*;
6
7/// CapeArrayEnumerationIn wraps an ICapeArrayEnumeration interface pointer.
8///
9/// Given a reference to an ICapeArrayEnumeration interface pointer, this allows setting
10///  and getting the elements.
11///
12/// This interface is typically used as arguments to rust methods
13/// on traits that are generated from CAPE-OPEN interfaces that have
14/// ICapeArrayEnumeration input arguments.
15///
16/// This class takes a mutable reference to the interface pointer, as
17/// it should be the only class that is in use at a time to change the 
18/// data behind the interface (as the data pointer is cached)
19///
20/// A NULL pointer is treated as an empty array.
21///
22/// # Examples
23///
24/// ```
25/// use cobia::*;
26///
27/// fn test_content(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
28///     assert_eq!(a.as_vec(), vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
29/// }
30/// 
31/// let arr = cobia::CapeArrayEnumerationVec::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
32/// test_content(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
33/// ```
34
35pub struct CapeArrayEnumerationIn<'a,Element:Copy+Clone> {
36	interface: &'a *mut C::ICapeArrayEnumeration,
37	data: *mut Element,
38	size: C::CapeSize,
39}
40
41impl<'a,Element:Copy+Clone> CapeArrayEnumerationIn<'a,Element> {
42	/// Create a new CapeArrayEnumerationIn from an ICapeArrayEnumeration interface pointer.
43	///
44	/// # Arguments
45	///
46	/// * `interface` - A pointer to an ICapeArrayEnumeration interface
47	///
48	/// # Examples
49	///
50	/// ```
51	/// use cobia::*;
52	/// use cobia::prelude::*;
53	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
54	///	let i_cape_array_enumeration=arr.as_cape_array_enumeration_in();
55	///	let mut i_cape_array_enumeration_ptr=(&i_cape_array_enumeration as *const C::ICapeArrayEnumeration).cast_mut(); //normally a pointer to the interface is received
56	///	let a = cobia::CapeArrayEnumerationIn::<cobia::CapePMCServiceType>::new(&mut i_cape_array_enumeration_ptr); //CapeArrayEnumerationIn from *mut C::ICapeArrayEnumeration
57	/// assert_eq!(a.as_vec(), vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
58	/// ```
59
60	pub fn new(interface: &'a *mut C::ICapeArrayEnumeration) -> Self {
61		if interface.is_null() {
62			return Self {
63				interface,
64				data: std::ptr::null_mut(),
65				size: 0,
66			};
67		}
68		let mut data: *mut Element = std::ptr::null_mut();
69		let mut size: C::CapeSize = 0;
70		unsafe { (*(**interface).vTbl).get.unwrap()((**interface).me,&mut data as *mut *mut Element as *mut *mut i32,&mut size as *mut C::CapeSize) };
71		Self {
72			interface,
73			data,
74			size,
75		}
76	}
77
78	/// Return the size of the array
79	///
80	/// # Examples
81	///
82	/// ```
83	/// use cobia::*;
84	///
85	/// fn test_size(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
86	///     assert_eq!(a.size(), 2);
87	/// }
88	/// 
89	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
90	/// test_size(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
91	/// ```
92	pub fn size(&self) -> usize {
93		self.size as usize
94	}
95
96	/// Check if the array is empty
97	///
98	/// # Examples
99	///
100	/// ```
101	/// use cobia::*;
102	///
103	/// fn test_empty(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
104	///     assert!(a.is_empty());
105	/// }
106	/// 
107	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::new();
108	/// test_empty(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
109	/// ```
110	pub fn is_empty(&self) -> bool {
111		self.size == 0
112	}
113	
114	/// Return the content of the array as a vector.
115	///
116	/// # Examples
117	///
118	/// ```
119	/// use cobia::*;
120	///
121	/// fn test_content(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
122	///     assert_eq!(a.as_vec(), vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
123	/// }
124	/// 
125	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
126	/// test_content(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
127	/// ```
128
129	pub fn as_vec(&self) -> Vec<Element> {
130		let slice = unsafe { std::slice::from_raw_parts(self.data, self.size as usize) };
131		slice.to_vec()
132	}
133
134	/// Return the content of the real array as a real slice.
135	///
136	/// # Examples
137	///
138	/// ```
139	/// use cobia::*;
140	///
141	/// fn test_content(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
142	///     assert_eq!(a.as_slice(), &[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
143	/// }
144	/// 
145	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
146	/// test_content(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
147	/// ```
148
149	pub fn as_slice(&self) -> &[Element] {
150		let slice = unsafe { std::slice::from_raw_parts(self.data, self.size as usize) };
151		slice
152	}
153
154	/// Return an iterator for the array.
155	///
156	/// # Examples
157	///
158	/// ```
159	/// use cobia::*;
160	///
161	/// fn test_iter(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
162	///		let mut iter = a.iter();
163	///		assert_eq!(iter.next().unwrap(), cobia::CapePMCServiceType::Inproc64);
164	///		assert_eq!(iter.next().unwrap(), cobia::CapePMCServiceType::COM64);
165	///		assert!(!iter.next().is_some());
166	/// }
167	/// 
168	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
169	/// test_iter(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
170	/// ```
171
172	pub fn iter(&self) -> CapeArrayRefIterator<'_,Element> {
173		CapeArrayRefIterator {
174			data: &self.as_slice(),
175			index: 0
176		}
177	}
178
179}
180
181impl<'a,Element:Copy+Clone> std::ops::Index<usize> for CapeArrayEnumerationIn<'a,Element> {
182	type Output = Element;
183
184	/// Indexing
185	///
186	/// Returns a reference to the string at the given index.
187	///
188	/// # Arguments
189	///
190	/// * `index` - The index of the string to be returned
191	///
192	/// # Examples
193	///
194	/// ```rust
195	/// use cobia::*;
196	///
197	/// fn test_item(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
198	///		assert_eq!(a[1], cobia::CapePMCServiceType::COM64);
199	/// }
200	/// 
201	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
202	/// test_item(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
203	/// ```
204
205	fn index(&self, index: usize) -> &Self::Output {
206		if index>=(self.size as usize) {
207			panic!("index out of bounds");
208		}
209		unsafe { &*self.data.add(index) as &Element }
210	}
211
212}
213
214impl<'a,Element:Copy+Clone+std::fmt::Display> fmt::Display for CapeArrayEnumerationIn<'a,Element> {
215
216	/// Display the content of the real array as a real vector.
217	///
218	/// # Examples
219	///
220	/// ```
221	/// use cobia::*;
222	///
223	/// fn test_format(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
224	///     assert_eq!(format!("{}", a), "[Inproc64, COM64, Local]");
225	/// }
226	/// 
227	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64,cobia::CapePMCServiceType::Local]);
228	/// test_format(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
229	/// ```
230
231    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
232        write!(f, "[")?;
233        for (count, v) in self.iter().enumerate() {
234            if count != 0 { write!(f, ", ")?; }
235            write!(f, "{}", v)?;
236        }
237        write!(f, "]")
238    }
239}
240
241
242impl<'a,Element:Copy+Clone+'a> IntoIterator for &'a CapeArrayEnumerationIn<'a,Element> {
243	type Item = Element;
244	type IntoIter = CapeArrayRefIterator<'a, Element>;
245
246	/// Return an iterator over the real array.
247	///
248	/// # Examples
249	///
250	/// ```
251	/// use cobia::*;
252	///
253	/// fn test_iter(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
254	///		let mut hasCOM64=false;
255	///		for val in a {
256	///		 if val==cobia::CapePMCServiceType::COM64 {
257	///			    hasCOM64=true;
258	///		 }
259	///		}
260	///		assert!(hasCOM64);
261	/// }
262	/// 
263	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
264	/// test_iter(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
265	/// ```
266
267	fn into_iter(self) -> CapeArrayRefIterator<'a, Element> {
268		CapeArrayRefIterator {
269			data: self.as_slice(),
270			index: 0,
271		}
272	}
273}
274
275/// An iterator that consumes a CapeArrayEnumerationIn
276///
277/// This struct is created by the IntoIterator trait on CapeArrayEnumerationIn.
278///
279/// # Examples
280///
281/// ```
282/// use cobia::*;
283///
284/// fn test_iter(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
285///		let mut hasCOM64=false;
286///		for val in a {
287///		 if val==cobia::CapePMCServiceType::COM64 {
288///			    hasCOM64=true;
289///		 }
290///		}
291///		assert!(hasCOM64);
292/// }
293/// 
294/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
295/// test_iter(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
296/// ```
297
298pub struct CapeArrayEnumerationIteratorIn<'a,Element:Copy+Clone> {
299	arr: CapeArrayEnumerationIn<'a,Element>,
300	index: usize
301}
302
303impl<'a,Element:Copy+Clone> Iterator for CapeArrayEnumerationIteratorIn<'a,Element> {
304	type Item = Element;
305	fn next(&mut self) -> Option<Self::Item> {
306		if self.index < (self.arr.size as usize) {
307			let res = unsafe{ *self.arr.data.add(self.index)};
308			self.index += 1;
309			Some(res)
310		} else {
311			None
312		}
313	}
314}
315
316
317impl<'a,Element:Copy+Clone> IntoIterator for CapeArrayEnumerationIn<'a,Element> {
318	type Item = Element;
319	type IntoIter = CapeArrayEnumerationIteratorIn<'a,Element>;
320
321	/// Return an iterator over the real array.
322	///
323	/// # Examples
324	///
325	/// ```
326	/// use cobia::*;
327	///
328	/// fn test_iter(a: &CapeArrayEnumerationIn<cobia::CapePMCServiceType>) {
329	///		let mut hasCOM64=false;
330	///		for val in a {
331	///		 if val==cobia::CapePMCServiceType::COM64 {
332	///			    hasCOM64=true;
333	///		 }
334	///		}
335	///		assert!(hasCOM64);
336	/// }
337	/// 
338	/// let arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
339	/// test_iter(&CapeArrayEnumerationInFromProvider::from(&arr).as_cape_array_enumeration_in());
340	/// ```
341
342	fn into_iter(self) -> Self::IntoIter {
343		CapeArrayEnumerationIteratorIn {
344			arr: self,
345			index: 0,
346		}
347	}
348}
349
350impl<'a,Element:Copy+Clone> CapeArrayEnumerationProviderIn for CapeArrayEnumerationIn<'a,Element> {
351	fn as_cape_array_enumeration_in(&self) -> C::ICapeArrayEnumeration {
352		unsafe { **self.interface }
353	}
354}
355
356/// CapeArrayEnumerationOut wraps an ICapeArrayEnumeration interface pointer.
357///
358/// Given an ICapeArrayEnumeration interface pointer, this allows setting
359///  and getting the elements.
360///
361/// A NULL pointer is not allowed.
362/// 
363/// This interface is typically used as arguments to rust methods
364/// on traits that are generated from CAPE-OPEN interfaces that have
365/// ICapeArrayEnumeration ouput arguments.
366///
367/// This class takes a mutable reference to the interface pointer, as
368/// it should be the only class that is in use at a time to change the 
369/// data behind the interface (as the data pointer is cached)
370///
371/// # Examples
372///
373/// ```
374/// use cobia::*;
375///
376/// fn set_content(a: &mut CapeArrayEnumerationOut<CapePMCServiceType>) {
377///		a.put_array(&[CapePMCServiceType::Inproc64,CapePMCServiceType::COM64]).unwrap();
378/// }
379/// 
380/// let mut arr = cobia::CapeArrayEnumerationVec::<CapePMCServiceType>::new();
381/// set_content(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
382/// assert_eq!(arr.as_vec(), &vec![CapePMCServiceType::Inproc64,CapePMCServiceType::COM64]);
383/// ```
384
385pub struct CapeArrayEnumerationOut<'a,Element:Copy+Clone> {
386	interface: &'a mut *mut C::ICapeArrayEnumeration,
387	data: *mut Element,
388	size: C::CapeSize,
389}
390
391impl<'a,Element:Copy+Clone> CapeArrayEnumerationOut<'a,Element> {
392	/// Create a new CapeArrayEnumerationOut from an ICapeArrayEnumeration interface pointer.
393	///
394	/// # Arguments
395	///
396	/// * `interface` - A pointer to an ICapeArrayEnumeration interface
397	///
398	/// # Examples
399	///
400	/// ```
401	/// use cobia::*;
402	/// use cobia::prelude::*;
403	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
404	///	let i_cape_array_enumeration=arr.as_cape_array_enumeration_out();
405	///	let mut i_cape_array_enumeration_ptr=(&i_cape_array_enumeration as *const C::ICapeArrayEnumeration).cast_mut(); //normally a pointer to the interface is received
406	///	let a = cobia::CapeArrayEnumerationOut::<cobia::CapePMCServiceType>::new(&mut i_cape_array_enumeration_ptr); //CapeArrayEnumerationOut from *mut C::ICapeArrayEnumeration
407	/// assert_eq!(a.as_vec(), vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
408	/// ```
409
410	pub fn new(interface: &'a mut *mut C::ICapeArrayEnumeration) -> Self {
411		if interface.is_null() {
412			panic!("CapeArrayEnumerationOut cannot be created from a NULL pointer");
413		}
414		let mut data: *mut Element = std::ptr::null_mut();
415		let mut size: C::CapeSize = 0;
416		unsafe { (*(**interface).vTbl).get.unwrap()((**interface).me,&mut data as *mut *mut Element as *mut *mut i32,&mut size as *mut C::CapeSize) };
417		Self {
418			interface,
419			data,
420			size,
421		}
422	}
423
424	/// Get the size of the array
425	///
426	/// # Examples
427	///
428	/// ```
429	/// use cobia::*;
430	///
431	/// fn test_size(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
432	///     assert_eq!(a.size(), 2);
433	/// }
434	/// 
435	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
436	/// test_size(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
437	/// ```
438	pub fn size(&self) -> usize {
439		self.size as usize
440	}
441
442	/// Check if the array is empty
443	///
444	/// # Examples
445	///
446	/// ```
447	/// use cobia::*;
448	///
449	/// fn test_empty(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
450	///     assert!(a.is_empty());
451	/// }
452	/// 
453	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::new();
454	/// test_empty(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
455	/// ```
456	pub fn is_empty(&self) -> bool {
457		self.size == 0
458	}
459	
460	/// Return the content of the array as a vector.
461	///
462	/// # Examples
463	///
464	/// ```
465	/// use cobia::*;
466	///
467	/// fn test_content(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
468	///    assert_eq!(a.as_vec(), vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
469	/// }
470	/// 
471	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
472	/// test_content(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
473	/// ```
474
475	pub fn as_vec(&self) -> Vec<Element> {
476		let slice = unsafe { std::slice::from_raw_parts(self.data, self.size as usize) };
477		slice.to_vec()
478	}
479
480	/// Set the content of the array from a slice
481	///
482	/// # Arguments
483	///
484	/// * `arr` - A slice or array of vector
485	///
486	/// # Examples
487	///
488	/// ```
489	/// use cobia::*;
490	///
491	/// fn set_content(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
492	///    a.put_array(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]).unwrap();
493	/// }
494	/// 
495	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
496	/// set_content(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
497	/// assert_eq!(arr.as_vec(), &vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]); //the values have been stored on the object that implements ICapeArrayEnumeration<cobia::CapePMCServiceType>
498	/// ```
499
500	pub fn put_array(&mut self, array: &[Element]) -> Result<(), COBIAError> {
501		let res=unsafe {( *((**self.interface).vTbl) ).setsize.unwrap()((**self.interface).me,array.len() as C::CapeSize,&mut self.data as *mut *mut Element as *mut *mut i32)};
502		if res == COBIAERR_NOERROR {
503			self.size = array.len() as C::CapeSize;
504			for (i, val) in array.iter().enumerate() {
505				unsafe { *self.data.add(i as usize) = *val };
506			}
507			Ok(())
508		} else {
509			Err(COBIAError::Code(res))
510		}
511	}
512
513	/// Resize the array
514	///
515	/// # Arguments
516	///
517	/// * `size` - The new size of the array
518	///
519	/// # Examples
520	///
521	/// ```
522	/// use cobia::*;
523	///
524	/// fn resize(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
525	///    a.resize(4).unwrap();
526	/// }
527	/// 
528	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_slice(&[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
529	/// resize(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
530	/// assert_eq!(arr.size(), 4); 
531	/// ```
532
533	pub fn resize(&mut self, size: usize) -> Result<(), COBIAError> {
534		let res=unsafe {( *((**self.interface).vTbl) ).setsize.unwrap()((**self.interface).me,size as C::CapeSize,&mut self.data as *mut *mut Element as *mut *mut i32)};
535		if res == COBIAERR_NOERROR {
536			self.size = size as C::CapeSize;
537			Ok(())
538		} else {
539			Err(COBIAError::Code(res))
540		}
541	}
542
543	/// Return the content of the real array as a real slice.
544	///
545	/// # Examples
546	///
547	/// ```
548	/// use cobia::*;
549	///
550	/// fn test_content(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
551	///    assert_eq!(a.as_slice(), &[cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
552	/// }
553	/// 
554	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
555	/// test_content(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
556	/// ```
557
558	pub fn as_slice(&self) -> &[Element] {
559		let slice = unsafe { std::slice::from_raw_parts(self.data, self.size as usize) };
560		slice
561	}
562
563	/// Return an iterator for the array.
564	///
565	/// # Examples
566	///
567	/// ```
568	/// use cobia::*;
569	///
570	/// fn test_iter(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
571	///    let mut iter = a.iter();
572	///		assert_eq!(iter.next().unwrap(), cobia::CapePMCServiceType::Inproc64);
573	///		assert_eq!(iter.next().unwrap(), cobia::CapePMCServiceType::COM64);
574	///		assert!(!iter.next().is_some());
575	/// }
576	/// 
577	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
578	/// test_iter(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
579	/// ```
580
581	pub fn iter(&self) -> CapeArrayRefIterator<'_,Element> {
582		CapeArrayRefIterator {
583			data: &self.as_slice(),
584			index: 0
585		}
586	}
587
588}
589
590impl<'a,Element:Copy+Clone> std::ops::Index<usize> for CapeArrayEnumerationOut<'a,Element> {
591	type Output = Element;
592
593	/// Indexing
594	///
595	/// Returns a reference to the string at the given index.
596	///
597	/// # Arguments
598	///
599	/// * `index` - The index of the string to be returned
600	///
601	/// # Examples
602	///
603	/// ```rust
604	/// use cobia::*;
605	///
606	/// fn test_index(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
607	///    assert_eq!(a[1], cobia::CapePMCServiceType::COM64);
608	/// }
609	/// 
610	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
611	/// test_index(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
612	/// ```
613
614	fn index(&self, index: usize) -> &Self::Output {
615		if index>=(self.size as usize) {
616			panic!("index out of bounds");
617		}
618		unsafe { &*self.data.add(index) as &Element }
619	}
620
621}
622
623impl<'a,Element:Copy+Clone> std::ops::IndexMut<usize> for CapeArrayEnumerationOut<'a,Element> {
624	/// Indexing
625	///
626	/// Returns a mutable reference to the string at the given index.
627	///
628	/// # Arguments
629	///
630	/// * `index` - The index of the string to be returned
631	///
632	/// # Examples
633	///
634	/// ```
635	/// use cobia::*;
636	///
637	/// fn test_content(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
638	///    assert_eq!(a.as_vec(), vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM32]);
639	/// }
640	/// 
641	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM32]);
642	/// test_content(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
643	/// ```
644
645	fn index_mut(&mut self, index: usize) -> &mut Self::Output {
646		if index>=(self.size as usize) {
647			panic!("index out of bounds");
648		}
649		unsafe { &mut *self.data.add(index) as &mut Element }
650	}
651}
652
653
654impl<'a,Element:Copy+Clone+std::fmt::Display> fmt::Display for CapeArrayEnumerationOut<'a,Element> {
655
656	/// Display the content of the real array as a real vector.
657	///
658	/// # Examples
659	///
660	/// ```
661	/// use cobia::*;
662	///
663	/// fn test_format(a: &mut CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
664	///    assert_eq!(format!("{}", a), "[Inproc64, COM64, Local]");
665	/// }
666	/// 
667	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64,cobia::CapePMCServiceType::Local]);
668	/// test_format(&mut CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
669	/// ```
670
671    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
672        write!(f, "[")?;
673        for (count, v) in self.iter().enumerate() {
674            if count != 0 { write!(f, ", ")?; }
675            write!(f, "{}", v)?;
676        }
677        write!(f, "]")
678    }
679}
680
681
682impl<'a,Element:Copy+Clone+'a> IntoIterator for &'a CapeArrayEnumerationOut<'a,Element> {
683	type Item = Element;
684	type IntoIter = CapeArrayRefIterator<'a, Element>;
685
686	/// Return an iterator over the real array.
687	///
688	/// # Examples
689	///
690	/// ```
691	/// use cobia::*;
692	///
693	/// fn test_iter(a: CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
694	///		let mut hasCOM64=false;
695	///		for val in a {
696	///		    if val==cobia::CapePMCServiceType::COM64 {
697	///		        hasCOM64=true;
698	///		    }
699	///		}
700	///		assert!(hasCOM64);
701	/// }
702	/// 
703	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
704	/// test_iter(CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
705	/// ```
706
707	fn into_iter(self) -> CapeArrayRefIterator<'a, Element> {
708		CapeArrayRefIterator {
709			data: self.as_slice(),
710			index: 0,
711		}
712	}
713}
714
715/// An iterator that consumes a CapeArrayEnumerationOut
716///
717/// This struct is created by the IntoIterator trait on CapeArrayEnumerationOut.
718///
719/// # Examples
720///
721/// ```
722/// use cobia::*;
723///
724/// fn test_iter(a: CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
725///		let mut hasCOM64=false;
726///		for val in a {
727///		    if val==cobia::CapePMCServiceType::COM64 {
728///		        hasCOM64=true;
729///		    }
730///		}
731///		assert!(hasCOM64);
732/// }
733/// 
734/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
735/// test_iter(CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
736/// ```
737
738
739pub struct CapeArrayEnumerationIteratorOut<'a,Element:Copy+Clone> {
740	arr: CapeArrayEnumerationOut<'a,Element>,
741	index: usize
742}
743
744impl<'a,Element:Copy+Clone> Iterator for CapeArrayEnumerationIteratorOut<'a,Element> {
745	type Item = Element;
746	fn next(&mut self) -> Option<Self::Item> {
747		if self.index < (self.arr.size as usize) {
748			let res = unsafe{ *self.arr.data.add(self.index)};
749			self.index += 1;
750			Some(res)
751		} else {
752			None
753		}
754	}
755}
756
757
758impl<'a,Element:Copy+Clone> IntoIterator for CapeArrayEnumerationOut<'a,Element> {
759	type Item = Element;
760	type IntoIter = CapeArrayEnumerationIteratorOut<'a,Element>;
761
762	/// Return an iterator over the real array.
763	///
764	/// # Examples
765	///
766	/// ```
767	/// use cobia::*;
768	///
769	/// fn test_iter(a: CapeArrayEnumerationOut<cobia::CapePMCServiceType>) {
770	///		let mut hasCOM64=false;
771	///		for val in a {
772	///		    if val==cobia::CapePMCServiceType::COM64 {
773	///		        hasCOM64=true;
774	///		    }
775	///		}
776	///		assert!(hasCOM64);
777	/// }
778	/// 
779	/// let mut arr = cobia::CapeArrayEnumerationVec::<cobia::CapePMCServiceType>::from_vec(vec![cobia::CapePMCServiceType::Inproc64,cobia::CapePMCServiceType::COM64]);
780	/// test_iter(CapeArrayEnumerationOutFromProvider::from(&mut arr).as_cape_array_enumeration_out());
781	/// ```
782
783	fn into_iter(self) -> Self::IntoIter {
784		CapeArrayEnumerationIteratorOut {
785			arr: self,
786			index: 0,
787		}
788	}
789}
790
791impl<'a,Element:Copy+Clone> CapeArrayEnumerationProviderOut for CapeArrayEnumerationOut<'a,Element> {
792	fn as_cape_array_enumeration_out(&mut self) -> C::ICapeArrayEnumeration {
793		unsafe { **self.interface }
794	}
795}