Skip to main content

ClassBox

Struct ClassBox 

Source
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>

Source

pub fn leak<'a>(self) -> &'a mut C

Leak the class, returning a reference to that class

Source

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.

Source

pub fn upcast(self) -> ClassBox<C::Target>
where C: DerefMut, C::Target: Class,

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§

Source§

impl<C: Debug + ?Sized + Class> Debug for ClassBox<C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: ?Sized + Class> Deref for ClassBox<C>

Source§

type Target = C

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<C: ?Sized + Class> DerefMut for ClassBox<C>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: ?Sized + Class, U: ?Sized + Class> Downcast<U> for ClassBox<T>
where for<'a> &'a mut T: Downcast<U, Wrapped = &'a mut U>,

Source§

type Wrapped = ClassBox<U>

Source§

fn downcast(self) -> Result<ClassBox<U>, Self>

Downcast into a child class. Returns Err(self) if the cast is invalid.
Source§

impl<C: ?Sized + Class> Drop for ClassBox<C>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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>
where C: Send + ?Sized,

§

impl<C> Sync for ClassBox<C>
where C: Sync + ?Sized,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> DowncastTo for T

Source§

fn downcast_to<T: ?Sized>(self) -> Option<Self::Wrapped>
where Self: Downcast<T>,

Downcast to child class T. Returns None if the cast is invalid.
Source§

fn downcast_or_err<T: ?Sized>(self) -> Result<Self::Wrapped, Self>
where Self: Downcast<T>,

Downcast to child class T. Returns Err(self) if the cast is invalid.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.