沿多个结构共享 &str 与生命周期冲突

发布于 2025-01-15 01:59:14 字数 2240 浏览 3 评论 0 原文

我有以下代码:

pub trait Regex: RegexClone {
    fn check(&self) -> Result<u32,(/* errors should detail where it fails*/)>;
    fn next(&self) -> Option<Box<dyn Regex>>;
}

pub trait RegexClone {
    fn regex_clone(&self) -> Box<dyn Regex>;
}
    
pub struct PatternAnyCharacter<'a>{
    string: &'a str
}
    
impl RegexClone for PatternAnyCharacter<'_> {
    fn regex_clone(&self) -> Box<dyn Regex> {
        return Box::new(PatternAnyCharacter {string: self.string})
    }
}

impl Regex for PatternAnyCharacter<'_> {
    fn check(&self) -> Result<u32, ()> {
        if self.string.len() > 0 {
            return Ok(1);
        }
        Err(())
    }

    fn next(&self) ->  Option<Box<dyn Regex>> {
        None
    }
}

这个想法是,当我调用 regex_clone 时,我会得到一个新的 Box使用相同的 &str 作为成员,我认为因为我在调用 regex_clone 时只使用不可变引用,所以它会给我一个具有相同字符串切片的新结构,因为是一个引用,我没有移动任何东西,但是编译器抱怨以下内容:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src/lib.rs:63:25
   |
63 |         return Box::new(PatternAnyCharacter {string: self.string})
   |                         ^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'_` as defined here...
  --> src/lib.rs:61:41
   |
61 | impl RegexClone for PatternAnyCharacter<'_> {
   |                                         ^^
note: ...so that reference does not outlive borrowed content
  --> src/lib.rs:63:54
   |
63 |         return Box::new(PatternAnyCharacter {string: self.string})
   |                                                      ^^^^^^^^^^^
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
  --> src/lib.rs:63:16
   |
63 |         return Box::new(PatternAnyCharacter {string: self.string})
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected `Box<(dyn Regex + 'static)>`
              found `Box<dyn Regex>`

我该如何解决这个问题,以便我可以与多个结构共享相同的字符串切片? 我考虑过将字符串切片完全作为成员并将其作为参数传递以进行检查,但希望我可以避免它。

I have the following code:

pub trait Regex: RegexClone {
    fn check(&self) -> Result<u32,(/* errors should detail where it fails*/)>;
    fn next(&self) -> Option<Box<dyn Regex>>;
}

pub trait RegexClone {
    fn regex_clone(&self) -> Box<dyn Regex>;
}
    
pub struct PatternAnyCharacter<'a>{
    string: &'a str
}
    
impl RegexClone for PatternAnyCharacter<'_> {
    fn regex_clone(&self) -> Box<dyn Regex> {
        return Box::new(PatternAnyCharacter {string: self.string})
    }
}

impl Regex for PatternAnyCharacter<'_> {
    fn check(&self) -> Result<u32, ()> {
        if self.string.len() > 0 {
            return Ok(1);
        }
        Err(())
    }

    fn next(&self) ->  Option<Box<dyn Regex>> {
        None
    }
}

The idea is that when i call regex_clone i get a new Box<dyn Regex> with the same &str as member, i supossed that since im only using inmutable references when calling regex_clone it would give me a new struct with the same string slice, since is a reference im not moving anything, however the compiler complains the following:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src/lib.rs:63:25
   |
63 |         return Box::new(PatternAnyCharacter {string: self.string})
   |                         ^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'_` as defined here...
  --> src/lib.rs:61:41
   |
61 | impl RegexClone for PatternAnyCharacter<'_> {
   |                                         ^^
note: ...so that reference does not outlive borrowed content
  --> src/lib.rs:63:54
   |
63 |         return Box::new(PatternAnyCharacter {string: self.string})
   |                                                      ^^^^^^^^^^^
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
  --> src/lib.rs:63:16
   |
63 |         return Box::new(PatternAnyCharacter {string: self.string})
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected `Box<(dyn Regex + 'static)>`
              found `Box<dyn Regex>`

How can i solve this so i can share the same string slice with multiple struct?
i thought about foregoing the string slice as member entirely and passing it as parameter to check, but hopefully i can avoid it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2025-01-22 01:59:14

如果您想允许它借用 &self 的部分内容,您需要定义返回的 dyn Regex 不能比 &self 更长久的特征.:(

pub trait RegexClone {
    fn regex_clone<'a>(&'a self) -> Box<dyn Regex + 'a>;
} 

您也可以使用匿名生命周期(Box),但这更容易理解。)

旁注:我不认为“克隆” “是这样一个正确的名字 功能。

You need to define at the trait that the returned dyn Regex can't outlive &self if you want to allow it to borrow from parts of &self.:

pub trait RegexClone {
    fn regex_clone<'a>(&'a self) -> Box<dyn Regex + 'a>;
} 

(You can also use an anonymous lifetime (Box<dyn Regex + '_>), but this is easier to understand.)

Side note: I don't think "clone" is the right name for such a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文