| Crates.io | ownedref |
| lib.rs | ownedref |
| version | 0.15.4 |
| created_at | 2025-11-13 01:40:03.136064+00 |
| updated_at | 2025-11-13 01:40:03.136064+00 |
| description | Library to pass around references that will be owned types on deserialization |
| homepage | |
| repository | https://github.com/AFLplusplus/LibAFL/ |
| max_upload_size | |
| id | 1930269 |
| size | 72,936 |
OwnedRef: Owned references for the massesThe ownedref crate provides a set of wrappers that abstract over accessing data that can be either owned or borrowed (referenced). The primary purpose of these wrappers is to enable serialization of data structures that contain references or raw pointers.
A key feature is the ability to serialize references by transparently converting any borrowed data to an owned form. This is particularly useful in applications like serialization for IPC or saving state, where we can't save memory addresses but need to preserve the data they point to.
When you have a struct that contains references, you can't directly serialize it with serde. ownedref provides wrapper types that can be used in place of references. These wrappers are enums that can hold either a reference or an owned value. When serializing, they always serialize the underlying data, and when deserializing, they create an owned value.
This allows you to work with references for performance within your application, and seamlessly serialize and deserialize your data structures when needed.
The crate provides the following wrappers:
OwnedRef<'a, T>: Wraps a &'a T or Box<T>.OwnedRefMut<'a, T>: Wraps a &'a mut T or Box<T>.OwnedSlice<'a, T>: Wraps a &'a [T] or Vec<T>.OwnedMutSlice<'a, T>: Wraps a &'a mut [T] or Vec<T>.OwnedMutSizedSlice<'a, T, const N: usize>: Wraps a &'a mut [T; N] or Box<[T; N]>.OwnedPtr<T>: Wraps a *const T or Box<T>.OwnedMutPtr<T>: Wraps a *mut T or Box<T>.Here are some examples of how to use the ownedref wrappers.
OwnedRefOwnedRef can be used to hold either a reference to a value or an owned value.
use ownedref::OwnedRef;
use serde::{Serialize, Deserialize};
// A struct that can be either borrowed or owned.
#[derive(Serialize, Deserialize, Debug)]
struct MyStruct<'a> {
data: OwnedRef<'a, [u8]>,
}
// Create an instance with a borrowed reference.
let data = vec![1, 2, 3, 4];
let borrowed_struct = MyStruct { data: OwnedRef::Ref(data.as_slice()) };
// You can access the data using `as_ref`.
assert_eq!(borrowed_struct.data.as_ref(), &[1, 2, 3, 4]);
// Serialize the struct. This will copy the data.
let serialized = serde_json::to_string(&borrowed_struct).unwrap();
println!("Serialized: {}", serialized);
// Deserialize the struct. This will create an owned value.
let deserialized_struct: MyStruct = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized_struct.data.as_ref(), &[1, 2, 3, 4]);
// The deserialized struct now owns the data.
match deserialized_struct.data {
OwnedRef::Owned(_) => println!("Data is owned."),
_ => panic!("Data should be owned after deserialization."),
}
OwnedSliceOwnedSlice is useful for slices of data.
use ownedref::OwnedSlice;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct DataContainer<'a> {
slice: OwnedSlice<'a, u32>,
}
// With a borrowed slice
let original_data = vec![10, 20, 30];
let container_borrowed = DataContainer {
slice: OwnedSlice::from(original_data.as_slice()),
};
assert_eq!(container_borrowed.slice.as_ref(), &[10, 20, 30]);
let serialized = serde_json::to_string(&container_borrowed).unwrap();
println!("Serialized: {}", serialized);
// Deserialize into an owned version
let container_deserialized: DataContainer = serde_json::from_str(&serialized).unwrap();
assert_eq!(container_deserialized.slice.as_ref(), &[10, 20, 30]);
assert!(container_deserialized.slice.is_owned());
LibAFL ProjectThe LibAFL project is part of AFLplusplus and maintained by
For bugs, feel free to open issues or contact us directly. Thank you for your support. <3
Even though we will gladly assist you in finishing up your PR, try to
cfgs.)cargo nightly fmt on your code before pushingcargo clippy --all or ./clippy.shcargo build --no-default-features to check for no_std compatibility (and possibly add #[cfg(feature = "std")]) to hide parts of your code.Some parts in this list may sound hard, but don't be afraid to open a PR if you cannot fix them by yourself. We will gladly assist.