-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create the concept of 'owned data' in upb/rust (similar to MOA but fo…
…r arbitrary types) and use that on the wire parse/serialize path. PiperOrigin-RevId: 626012269
- Loading branch information
1 parent
1d6fdc1
commit 5d22534
Showing
7 changed files
with
156 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::Arena; | ||
use std::fmt::{self, Debug}; | ||
use std::ops::Deref; | ||
|
||
/// An 'owned' T, conceptually similar to a Box<T> where the T is | ||
/// something in a upb Arena. By holding the data pointer and the owned arena | ||
/// together the data lifetime will be maintained. | ||
pub struct OwnedData<T: ?Sized> { | ||
data: *const T, | ||
arena: Arena, | ||
} | ||
|
||
impl<T: ?Sized> OwnedData<T> { | ||
/// Construct `OwnedData` from raw pointers and its owning arena. | ||
/// | ||
/// # Safety | ||
/// - `data` must have the lifetime of `arena` (e.g. it was allocated using | ||
/// `arena` or other arenas fused to it). | ||
/// - `data` must not mutate while this struct exists | ||
pub unsafe fn new(data: *const T, arena: Arena) -> Self { | ||
OwnedData { arena, data } | ||
} | ||
|
||
/// Gets a raw slice pointer. | ||
pub fn data(&self) -> *const T { | ||
self.data | ||
} | ||
|
||
pub fn as_ref(&self) -> &T { | ||
// SAFETY: | ||
// - `data` is valid under the conditions set on ::new(). | ||
unsafe { self.data.as_ref().unwrap() } | ||
} | ||
|
||
pub fn into_parts(self) -> (*const T, Arena) { | ||
(self.data, self.arena) | ||
} | ||
} | ||
|
||
impl<T> OwnedData<[T]> { | ||
pub unsafe fn from_raw_parts(arena: Arena, data: std::ptr::NonNull<T>, len: usize) -> Self { | ||
OwnedData::new(std::slice::from_raw_parts(data.as_ptr(), len), arena) | ||
} | ||
} | ||
|
||
impl<T: ?Sized> Deref for OwnedData<T> { | ||
type Target = T; | ||
fn deref(&self) -> &Self::Target { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl<T: ?Sized> AsRef<T> for OwnedData<T> { | ||
fn as_ref(&self) -> &T { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl<T: Debug> Debug for OwnedData<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::Debug::fmt(self.deref(), f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters