rust_3d/has_bounding_box_2d.rs
1/*
2Copyright 2016 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! HasBoundingBox2D and HasBoundingBox2DMaybe traits for types which (might) have a bounding box
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29/// HasBoundingBox2D is a trait for types which have a bounding box
30pub trait HasBoundingBox2D: HasBoundingBox2DMaybe {
31 /// Should return the bounding box if it can be calculated
32 fn bounding_box(&self) -> BoundingBox2D;
33}
34
35/// HasBoundingBox2DMaybe is a trait for types which might have a bounding box
36pub trait HasBoundingBox2DMaybe {
37 /// Should return the bounding box if it can be calculated
38 fn bounding_box_maybe(&self) -> Result<BoundingBox2D>;
39}
40
41//------------------------------------------------------------------------------
42
43/// Wrapper helper to convert the Maybe type to the non-Maybe type
44pub struct HasBoundingBox2DConverted<T>
45where
46 T: HasBoundingBox2DMaybe,
47{
48 data: T,
49}
50
51impl<T> HasBoundingBox2DConverted<T>
52where
53 T: HasBoundingBox2DMaybe,
54{
55 pub fn new(data: T) -> Result<Self> {
56 data.bounding_box_maybe()?; // ensure present
57 Ok(Self { data })
58 }
59
60 pub fn get(&self) -> &T {
61 &self.data
62 }
63}
64
65impl<T> HasBoundingBox2DMaybe for HasBoundingBox2DConverted<T>
66where
67 T: HasBoundingBox2DMaybe,
68{
69 fn bounding_box_maybe(&self) -> Result<BoundingBox2D> {
70 self.data.bounding_box_maybe()
71 }
72}
73
74impl<T> HasBoundingBox2D for HasBoundingBox2DConverted<T>
75where
76 T: HasBoundingBox2DMaybe,
77{
78 fn bounding_box(&self) -> BoundingBox2D {
79 self.data.bounding_box_maybe().unwrap() // safe since enforced on construction
80 }
81}