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