add Vector::clear

This commit is contained in:
soruh 2023-08-15 12:35:20 +02:00
parent cb908c8198
commit 3e37286621
2 changed files with 19 additions and 2 deletions

View File

@ -32,10 +32,17 @@ impl Default for Str {
} }
impl Str { impl Str {
#[must_use]
pub fn clear<R>(self, transaction: &mut TransactionHandle<R>) -> Self {
Self(self.0.clear(transaction))
}
#[must_use]
pub fn set<R>(self, transaction: &mut TransactionHandle<R>, s: &str) -> Self { pub fn set<R>(self, transaction: &mut TransactionHandle<R>, s: &str) -> Self {
Self(self.0.set(transaction, s.as_bytes())) Self(self.0.set(transaction, s.as_bytes()))
} }
#[must_use]
pub fn get(self, reader: &impl ReaderTrait) -> &str { pub fn get(self, reader: &impl ReaderTrait) -> &str {
std::str::from_utf8(self.0.get(reader)).unwrap() std::str::from_utf8(self.0.get(reader)).unwrap()
} }

View File

@ -34,9 +34,19 @@ impl Default for Vector {
} }
impl Vector { impl Vector {
#[must_use]
pub fn clear<R>(self, transaction: &mut TransactionHandle<R>) -> Self {
if !self.data.start.is_null() {
transaction.free_range(self.data);
}
Self::new()
}
#[must_use]
pub fn set<R>(self, transaction: &mut TransactionHandle<R>, s: &[u8]) -> Self { pub fn set<R>(self, transaction: &mut TransactionHandle<R>, s: &[u8]) -> Self {
let data = if s.is_empty() { let data = if s.is_empty() {
Vector::new().data Self::new().data
} else { } else {
let (range, data) = transaction.allocate_range(s.len() as u64); let (range, data) = transaction.allocate_range(s.len() as u64);
data.copy_from_slice(s.as_bytes()); data.copy_from_slice(s.as_bytes());
@ -47,7 +57,7 @@ impl Vector {
transaction.free_range(self.data); transaction.free_range(self.data);
} }
Vector { data } Self { data }
} }
pub fn get(self, reader: &impl ReaderTrait) -> &[u8] { pub fn get(self, reader: &impl ReaderTrait) -> &[u8] {