-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: various helper functions and fixes for platform contested resources PR #307
Changes from all commits
c4201ed
a81de5e
8b09e18
a8f02d2
a62699d
2c0712b
9cd00f3
6af586b
9fd8410
1c7d13d
761d45a
1601625
a4c65ba
5f75f85
d23d181
44a357c
56b824c
17e9193
229b7f2
d7474f4
3e581c8
c3d78a3
ca40298
b297918
07a5a19
b2483a0
c66cb90
964809f
0b32606
8664b72
31b5d21
ea4bf6a
8886695
c46194e
cb03383
adb8fd1
d77bbf5
c0d810e
61dc98a
1696c2e
53642ed
36eb5a9
c44aec6
5fb76c0
27e9a67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,12 +43,13 @@ | |
#[cfg(feature = "full")] | ||
use integer_encoding::VarInt; | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
use crate::reference_path::{path_from_reference_path_type, ReferencePathType}; | ||
#[cfg(any(feature = "full", feature = "verify"))] | ||
use crate::{element::SUM_ITEM_COST_SIZE, Element, Error}; | ||
#[cfg(feature = "full")] | ||
use crate::{ | ||
element::{SUM_TREE_COST_SIZE, TREE_COST_SIZE}, | ||
reference_path::{path_from_reference_path_type, ReferencePathType}, | ||
ElementFlags, | ||
}; | ||
|
||
|
@@ -64,15 +65,41 @@ | |
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Decoded the integer value in the SumItem element type, returns 0 for | ||
/// everything else | ||
/// Decoded the integer value in the SumItem element type | ||
pub fn as_sum_item_value(&self) -> Result<i64, Error> { | ||
match self { | ||
Element::SumItem(value, _) => Ok(*value), | ||
_ => Err(Error::WrongElementType("expected a sum item")), | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Decoded the integer value in the SumItem element type | ||
pub fn into_sum_item_value(self) -> Result<i64, Error> { | ||
match self { | ||
Element::SumItem(value, _) => Ok(value), | ||
_ => Err(Error::WrongElementType("expected a sum item")), | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Decoded the integer value in the SumTree element type | ||
pub fn as_sum_tree_value(&self) -> Result<i64, Error> { | ||
match self { | ||
Element::SumTree(_, value, _) => Ok(*value), | ||
_ => Err(Error::WrongElementType("expected a sum tree")), | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Decoded the integer value in the SumTree element type | ||
pub fn into_sum_tree_value(self) -> Result<i64, Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
match self { | ||
Element::SumTree(_, value, _) => Ok(value), | ||
_ => Err(Error::WrongElementType("expected a sum tree")), | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Gives the item value in the Item element type | ||
pub fn as_item_bytes(&self) -> Result<&[u8], Error> { | ||
|
@@ -91,6 +118,15 @@ | |
} | ||
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Gives the reference path type in the Reference element type | ||
pub fn into_reference_path_type(self) -> Result<ReferencePathType, Error> { | ||
match self { | ||
Element::Reference(value, ..) => Ok(value), | ||
_ => Err(Error::WrongElementType("expected a reference")), | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Check if the element is a sum tree | ||
pub fn is_sum_tree(&self) -> bool { | ||
|
@@ -103,6 +139,12 @@ | |
matches!(self, Element::SumTree(..) | Element::Tree(..)) | ||
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Check if the element is a reference | ||
pub fn is_reference(&self) -> bool { | ||
matches!(self, Element::Reference(..)) | ||
} | ||
|
||
#[cfg(any(feature = "full", feature = "verify"))] | ||
/// Check if the element is an item | ||
pub fn is_item(&self) -> bool { | ||
|
@@ -245,7 +287,7 @@ | |
#[cfg(feature = "full")] | ||
/// Get tree costs for a key value | ||
pub fn specialized_costs_for_key_value( | ||
key: &Vec<u8>, | ||
Check warning on line 290 in grovedb/src/element/helpers.rs GitHub Actions / clippywriting `&Vec` instead of `&[_]` involves a new object where a slice will do
|
||
value: &[u8], | ||
is_sum_node: bool, | ||
) -> Result<u32, Error> { | ||
|
@@ -316,9 +358,9 @@ | |
#[cfg(feature = "full")] | ||
/// Get the value defined cost for a serialized value | ||
pub fn value_defined_cost(&self) -> Option<ValueDefinedCostType> { | ||
let Some(value_cost) = self.get_specialized_cost().ok() else { | ||
return None; | ||
}; | ||
Check warning on line 363 in grovedb/src/element/helpers.rs GitHub Actions / clippythis `let...else` may be rewritten with the `?` operator
|
||
|
||
let cost = value_cost | ||
+ self.get_flags().as_ref().map_or(0, |flags| { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of this method?
as_sum_item_value
is just fine because i64 isCopy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still should be faster right as you are not copying memory. I agree it's not very important though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns from the function so the stack frame is cleared and memory will be copied from there anyways, we're not avoiding memory copies by using
self
unless we doing so to avoid doing explicit cloningsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look, it really doesn't matter, we have a lot of into and as. While I could remove this, I just don't think it matters, so I prefer to keep it.