pub struct ClassBox<C: ?Sized + Class> { /* private fields */ }Expand description
A wrapper around polymorphic Class types.
This wrapper aids in upcasting and downcasting, and is also used to make Drop work properly.
Internally, its a slightly-modified Box
Implementations§
Source§impl<C: ?Sized + Class> ClassBox<C>
impl<C: ?Sized + Class> ClassBox<C>
Sourcepub unsafe fn from_raw(raw: *mut C) -> ClassBox<C>
pub unsafe fn from_raw(raw: *mut C) -> ClassBox<C>
Reconstruct the class from a raw pointer to that class.
This is only safe is the raw pointer came from a Box or Box-derived allocation.
Sourcepub fn upcast(self) -> ClassBox<C::Target>
pub fn upcast(self) -> ClassBox<C::Target>
Upcast the class into its superclass.
Examples found in repository?
examples/readme_example.rs (line 125)
112fn main() {
113 // initialize an object. the `finish` method wraps the class in a `ClassBox`
114 // and allows it to behave polymorphically.
115 let object_two = ObjectTwo::new("hello!").finish();
116
117 // prints:
118 // ```
119 // hi from ObjectOne
120 // hello from ObjectTwo
121 // ```
122 object_two.print_example();
123 println!();
124
125 let object_as_one = object_two.upcast(); // is ClassBox<ObjectOne>
126
127 // calling a class method from anywhere in the class hierarchy will always result in the
128 // deepest implementation of that method getting executed. so, this also prints:
129 // ```
130 // hi from ObjectOne
131 // hello from ObjectTwo
132 // ```
133 object_as_one.print_example();
134 println!();
135
136 // you can call a method with the `my_` prefix in order to bypass the method overload and
137 // call that class's own implementation of the method. so this prints:
138 // ```
139 // hi from ObjectOne
140 // ```
141 object_as_one.my_print_example();
142 println!();
143
144 let mut object_as_base = object_as_one.upcast(); // is ClassBox<ObjectZero>
145
146 // this call results in all three `set_string` methods getting invoked
147 let new_str = "sets all strings!!";
148 object_as_base.set_string(new_str.to_string());
149
150 // you can't directly access a subclass's fields from a superclass. so, the following line
151 // would not compile:
152 // println!("{}" object_as_base.string_two);
153
154 let object_as_two = object_as_base
155 .downcast_to::<ObjectOne>().unwrap()
156 .downcast_to::<ObjectTwo>().unwrap();
157
158 // but, you can access a superclass's fields, within the limits set by visibilty rules
159 assert_eq!(object_as_two.string, new_str);
160 assert_eq!(object_as_two.string_one(), new_str);
161 assert_eq!(object_as_two.string_two, new_str);
162}Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for ClassBox<C>where
C: ?Sized,
impl<C> RefUnwindSafe for ClassBox<C>where
C: RefUnwindSafe + ?Sized,
impl<C> Send for ClassBox<C>
impl<C> Sync for ClassBox<C>
impl<C> Unpin for ClassBox<C>where
C: ?Sized,
impl<C> UnsafeUnpin for ClassBox<C>where
C: ?Sized,
impl<C> UnwindSafe for ClassBox<C>where
C: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more