rust: qom: add initial subset of methods on Object

Add an example of implementing instance methods and converting the
result back to a Rust type.  In this case the returned types are a
string (actually a Cow<str>; but that's transparent as long as it derefs
to &str) and a QOM class.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2024-11-06 00:01:57 +01:00
parent f50cd85c84
commit ba3b81f3b6
3 changed files with 66 additions and 3 deletions

View file

@ -88,6 +88,18 @@ fn test_object_new() {
}
}
#[test]
/// Try invoking a method on an object.
fn test_typename() {
init_qom();
let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() };
let p_ref: &DummyState = unsafe { &*p };
assert_eq!(p_ref.typename(), "dummy");
unsafe {
object_unref(p_ref.as_object_mut_ptr().cast::<c_void>());
}
}
// a note on all "cast" tests: usually, especially for downcasts the desired
// class would be placed on the right, for example:
//