-
Notifications
You must be signed in to change notification settings - Fork 23
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: add bytes_vec features #86
Conversation
Bound::Unbounded => 0, | ||
}; | ||
let end = match range.end_bound() { | ||
Bound::Included(&n) => n + 1, |
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.
I think it should be n
use std::ops::Bound::*;
use std::ops::RangeBounds;
fn main() {
assert_eq!((1..=12).end_bound(), Included(&12));
assert_eq!((1..13).end_bound(), Excluded(&13));
}
$ cargo run
...
$ echo $?
0
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.
this file is just reverted from #75 , I didn't look into the detail, @yangby-cryptape could you help to check?
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.
I think the current code is mostly correct. But there's an edge case, when n
is usize::MAX
. It might be better to pass the range to self.0[..].index
directly.
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.
Sorry, I misunderstood. It's correct.
use std::ops::Bound;
use std::ops::RangeBounds;
fn main() {
let a = vec![0, 1, 2, 3, 4, 5];
let exclude_2 = slice(a.clone(), 0..2);
let include_2 = slice(a.clone(), 0..=2);
let exclude_5 = slice(a.clone(), 0..5);
let include_5 = slice(a.clone(), 0..=5);
dbg!(&exclude_2);
dbg!(&include_2);
dbg!(&exclude_5);
dbg!(&include_5);
}
pub fn slice(src: Vec<u8>, range: impl RangeBounds<usize>) ->Vec<u8> {
let len = src.len();
let begin = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(&n) => n + 1,
Bound::Excluded(&n) => n,
Bound::Unbounded => len,
};
src[begin..end].to_vec()
}
cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/rust-playground`
[src/main.rs:21] &exclude_2 = [
0,
1,
]
[src/main.rs:22] &include_2 = [
0,
1,
2,
]
[src/main.rs:24] &exclude_5 = [
0,
1,
2,
3,
4,
]
[src/main.rs:25] &include_5 = [
0,
1,
2,
3,
4,
5,
]
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.
Use SliceIndex for (Bound, Bound)
let range = (range.start_bound.cloned(), range.end_bound().cloned());
self.0.index(range)
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.
Use SliceIndex for (Bound, Bound)
let range = (range.start_bound.cloned(), range.end_bound().cloned()); self.0.index(range)
But the SliceIndex trait is unsafe.
pub unsafe trait SliceIndex<T: ?Sized>: Sealed {
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's safe to use. It the same as self.0[range]
. (Actually, self.0[range]
is better.)
Personally, I have my doubts regarding this change. So just want to add my 2 cents here. There was a time where we had to use So to me, we should really advocate a path where the reference counted behavior of Bytes structure is fully celebrated. To me it might not be a good time to keep a path where we are emulating Bytes using plain And it's not like the only path available is to wait for 2nd hardfork, we do have a working path now where we can utilize Bytes right now without needing the hardfork, I personally wasn't sure a second workaround is needed here. So just to add my vote here: I wasn't sure if this is a good idea. |
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.
Codes are fine, but I don't want to make the decision to merge it, since I saw strongly differing opinions.
I keep neutral.
If a user wants to upgrade from 0.7 to 0.8 before the 2nd hardfork is activated, he has no choice but to adjust the old project configuration and compilation options. As molecule project maintainer, I think it would be more appropriate to give the user the option to keep the same behavior as in the 0.7 version (which is using vec). We can remove this feature flag after the second hardfork activation, maybe in 0.9 version. |
#75 introduced bytes crate in no_std env, which requires ckb2023 hardfork edition or modification on old contract projects.
This PR add a
bytes_vec
feature, make it easier to upgrading older projects that want to continue using vec: