gpt4 book ai didi

reference - 如何返回一个新创建的结构作为引用?

转载 作者:行者123 更新时间:2023-11-29 07:46:08 26 4
gpt4 key购买 nike

<分区>

作为学习 Rust 的练习,我决定实现一个位向量库,灵感来自 std::vec::Vec为哪些方法提供。

我有以下代码:

extern crate num;

use std::cmp::Eq;
use std::ops::{BitAnd,BitOrAssign,Index,Shl};
use num::{One,Zero,Unsigned,NumCast};

pub trait BitStorage: Sized +
BitAnd<Self, Output = Self> +
BitOrAssign<Self> +
Shl<Self, Output = Self> +
Eq + Zero + One + Unsigned + NumCast + Copy {}

impl<S> BitStorage for S where S: Sized +
BitAnd<S, Output = S> +
BitOrAssign<S> +
Shl<S, Output = S> +
Eq + Zero + One + Unsigned + NumCast + Copy {}

pub struct BitVector<S: BitStorage> {
data: Vec<S>,
capacity: usize,
storage_size: usize
}

impl<S: BitStorage> BitVector<S> {
pub fn with_capacity(capacity: usize) -> BitVector<S> {
let storage_size = std::mem::size_of::<S>() * 8;
let len = (capacity / storage_size) + 1;
BitVector {
data: vec![S::zero(); len],
capacity: capacity,
storage_size: storage_size
}
}

pub fn get(&self, index: usize) -> Option<bool> {
match self.index_in_bounds(index) {
true => Some(self.get_unchecked(index)),
false => None
}
}

pub fn set(&mut self, index: usize, value: bool) {
self.panic_index_bounds(index);
let (data_index, remainder) = self.compute_data_index_and_remainder(index);
let value = if value { S::one() } else { S::zero() };
self.data[data_index] |= value << remainder;
}

pub fn capacity(&self) -> usize {
self.capacity
}

pub fn split_at(&self, index: usize) -> (&BitVector<S>, &BitVector<S>) {
self.panic_index_not_on_storage_bound(index);
let data_index = self.compute_data_index(index);
let (capacity_left, capacity_right) = self.compute_capacities(index);
let (data_left, data_right) = self.data.split_at(data_index);

let left = BitVector {
data: data_left.to_vec(),
capacity: capacity_left,
storage_size: self.storage_size
};
let right = BitVector {
data: data_right.to_vec(),
capacity: capacity_right,
storage_size: self.storage_size
};
(&left, &right)
}

pub fn split_at_mut(&mut self, index: usize) -> (&mut BitVector<S>, &mut BitVector<S>) {
self.panic_index_not_on_storage_bound(index);
let data_index = self.compute_data_index(index);
let (capacity_left, capacity_right) = self.compute_capacities(index);
let (data_left, data_right) = self.data.split_at_mut(data_index);

let mut left = BitVector {
data: data_left.to_vec(),
capacity: capacity_left,
storage_size: self.storage_size
};
let mut right = BitVector {
data: data_right.to_vec(),
capacity: capacity_right,
storage_size: self.storage_size
};
(&mut left, &mut right)
}

#[inline]
fn get_unchecked(&self, index: usize) -> bool {
let (data_index, remainder) = self.compute_data_index_and_remainder(index);
(self.data[data_index] & (S::one() << remainder)) != S::zero()
}

#[inline]
fn compute_data_index_and_remainder(&self, index: usize) -> (usize, S) {
let data_index = self.compute_data_index(index);
let remainder = self.compute_data_remainder(index);
(data_index, remainder)
}

#[inline]
fn compute_data_index(&self, index: usize) -> usize {
index / self.storage_size
}

#[inline]
fn compute_data_remainder(&self, index: usize) -> S {
let remainder = index % self.storage_size;
// we know that remainder is always smaller or equal to the size that S can hold
// for example if S = u8 then remainder <= 2^8 - 1
let remainder: S = num::cast(remainder).unwrap();
remainder
}

#[inline]
fn compute_capacities(&self, index_to_split: usize) -> (usize, usize) {
(index_to_split, self.capacity - index_to_split)
}

#[inline]
fn index_in_bounds(&self, index: usize) -> bool {
index < self.capacity
}

#[inline]
fn panic_index_bounds(&self, index: usize) {
if !self.index_in_bounds(index) {
panic!("Index out of bounds. Length = {}, Index = {}", self.capacity, index);
}
}

#[inline]
fn panic_index_not_on_storage_bound(&self, index: usize) {
if index % self.storage_size != 0 {
panic!("Index not on storage bound. Storage size = {}, Index = {}", self.storage_size, index);
}
}
}

static TRUE: bool = true;
static FALSE: bool = false;

macro_rules! bool_ref {
($cond:expr) => (if $cond { &TRUE } else { &FALSE })
}

impl<S: BitStorage> Index<usize> for BitVector<S> {
type Output = bool;

fn index(&self, index: usize) -> &bool {
self.panic_index_bounds(index);
bool_ref!(self.get_unchecked(index))
}
}

编译器错误发生在 split_atsplit_at_mut方法:他们基本上告诉我 leftright在这两种情况下,都没有足够长的时间作为引用返回。我理解这一点,因为它们是在堆栈上创建的,然后我想将它们作为引用返回。

然而,我的设计灵感来自 std::vec::Vec你可以看到 in the SliceExt trait它们的定义如下:

#[stable(feature = "core", since = "1.6.0")]
fn split_at(&self, mid: usize) -> (&[Self::Item], &[Self::Item]);

#[stable(feature = "core", since = "1.6.0")]
fn split_at_mut(&mut self, mid: usize) -> (&mut [Self::Item], &mut [Self::Item]);

我想这样做是为了方便最终用户,因为他们宁愿处理引用而不是盒子。

我想我可以通过将返回的位向量放入 Box<_> 来修复我的错误, 但有没有办法将创建的结构作为引用返回?

作为奖励问题:如果我返回 (BitVector<S>, BitVector<S>),它确实有效,这样做有什么缺点?为什么 SliceExt特征不这样做?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com