-
Notifications
You must be signed in to change notification settings - Fork 1.3k
rewrite pybytearray with pybyteinner #916
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #916 +/- ##
========================================
+ Coverage 64.19% 64.4% +0.2%
========================================
Files 89 89
Lines 15229 15246 +17
Branches 3426 3434 +8
========================================
+ Hits 9777 9819 +42
+ Misses 3180 3154 -26
- Partials 2272 2273 +1
Continue to review full report at Codecov.
|
I had another idea about this. We face several types:
.. with a large set of similar methods:
.. and many more methods. I was thinking about some sort of a trait on the inner object. Would it be possible to de-duplicate the logic between all three python types by putting the logic in a trait like this: pub struct ByteInner {}
pub struct StrInner {}
pub trait StrMethods {
// This must be specifically implemented for ByteInner and StrInner:
fn Vec<char> get_chars();
fn set_chars(chars: Vec<char>);
// These are generic methods:
fn to_upper() {
let chars = self.get_chars();
// perform magic to upper here :)
self.set_chars(modified_chars);
}
// All other similar methods like swapcase and friends go here.
}
impl StrMethods for ByteInner {
// implement proper get_chars and set_chars
}
impl StrMethods for StrInner {
// implement proper get_chars and set_chars
} Would this be possible? |
While most bytes and bytearray methods can be merged, most bytes and str methods can't.
|
i'm not against the idea of the trait. but it adds much complexity due to many edge cases (like previous example). |
I'm for generic functions which can take [u8] or str arguments. We could use this for a whole series of string functions. Maybe we could do some templating with parameters like |
Something like this: fn ljust<T>(src: &[T], width: i32, fill_char: T) -> [T] {
// Implement ljust here...
} |
I quickly looked into it. Even it's intuitive, it's definitely not as easy/smart that it seems.
for all of this, I'm not sure we should try to do it. Even trying to reduce duplicate code, it (IMHO) will add to much complexity. |
I'm for readability at the expense of duplication. Maybe we will figure a way in the future to do this. |
This is a rewrite of bytearray with pybyteinner.
Everything is the same as in #847 , except :
Actual issues :
inner
and call directlyborrow
orborrow_mut
(see changes in io.rs). I need some help on it.append
...