我试图创建一个结构来操纵文件存储,但是更改值后将无法使用它。我确定这是一生,但我不知道该如何解决。
use std::error::Error;
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader};
use std::option::Option;
use std::path::Path;
pub struct Storage<'a> {
path_str: &'a str,
file: Option<File>,
}
const LOCKED_STORAGE: Storage<'static> = Storage {
path_str: &"/tmp/bmoneytmp.bms",
file: None,
};
pub fn get_instance() -> Storage<'static> {
if LOCKED_STORAGE.file.is_none() {
LOCKED_STORAGE.init();
}
LOCKED_STORAGE
}
impl Storage<'static> {
// Create a file for store all data, if does not alred exists
fn init(&mut self) {
let path = Path::new(self.path_str);
self.file = match OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(path)
{
Err(e) => panic!("Couldn't create the storage file at {}", e.description()),
Ok(file) => Some(file),
};
if self.file.is_none() {
panic!("Error on init??"); // This line is not called. Ok :)
}
}
// Check if section exists
pub fn check_section(&self, name: String) -> bool {
if self.file.is_none() {
panic!("Error on check??"); // This line is called. :(
}
true
}
}
fn main() {
let storage = get_instance();
storage.check_section("accounts".to_string());
}
此操作失败:
thread 'main' panicked at 'Error on check??', src/main.rs:48:13
我正在尝试使用一种方法来打开文件并读取此打开的文件,但是在第二种方法中,文件的实例未打开。使用Option<File>
,我用Same
/ None
更改了值,但变量仍然是None
。
答案 0 :(得分:1)
编程时,学习如何创建Minimal, Complete, and Verifiable example是一项非常重要的技能。这是您遇到的问题之一:
${x}
这将打印select p.page_id,p.page_title,pt.page_type_id,pt.page_type_name,e.exam_id,e.exam_name,m.menu_id,m.name,p.is_published,p.created_date,p.status_id from el_pages as p join el_page_types as pt on pt.page_type_id=p.page_type_id join el_exams as e on e.exam_id=p.exam_id join el_menus as m on m.menu_id=p.menu_id
-const EXAMPLE: Option<i32> = Some(42);
fn main() {
EXAMPLE.take();
println!("{:?}", EXAMPLE);
}
的值未更改。
Some(42)
变量不能保证会有多少个实例。允许编译器具有零个,一个或多个实例。每次使用EXAMPLE
时,就好像您在此处创建了一个全新的值,并粘贴了常量的定义:
const
相反,您想create a singleton。
另请参阅:
答案 1 :(得分:-1)
感谢@shepmaster。您的回答使我学到很多。但是我改变了方法,并使用Mutex全局static和lazy_static解决了问题。
我也阅读了文章https://bryce.fisher-fleig.org/blog/strategies-for-returning-references-in-rust/index.html,它有助于我理解自己的错误。
我的新代码:
#[macro_use]
extern crate lazy_static;
use std::error::Error;
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::option::Option;
use std::sync::Mutex;
pub struct Storage {
path_str: String,
file: Option<File>
}
lazy_static! {
pub static ref LOCKED_STORAGE: Mutex<Storage> = Mutex::new(start_storage());
}
fn start_storage() -> Storage {
let mut st = Storage { path_str: "/tmp/bmoneytmp.bms".to_string(), file: None };
st.init();
st
}
impl Storage {
// Create a file for store all data, if does not alred exists
fn init(&mut self) {
let path = Path::new(&self.path_str);
self.file = match OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(path)
{
Err(e) => panic!("Couldn't create the storage file at {}", e.description()),
Ok(file) => Some(file),
};
}
// Check if section exists
pub fn check_section(&self, name: String) -> bool {
let file = match &self.file {
Some(file) => file,
None => panic!("File of storage not opened")
};
for line in BufReader::new(file).lines() {
println!("{}{:?}", name, line); // Working!!
}
true
}
}
fn main() {
// run in your bash before: echo "Row 1" >> /tmp/bmoneytmp.bms
LOCKED_STORAGE.lock().unwrap().check_section("accounts".to_string());
}
我的项目的完整代码:https://github.com/fernandobatels/blitz-money
我的修复程序的完整代码:https://github.com/fernandobatels/blitz-money/blob/9dc04742a57e6cd99742f2400a6245f210521f5d/src/backend/storage.rs https://github.com/fernandobatels/blitz-money/blob/9dc04742a57e6cd99742f2400a6245f210521f5d/src/backend/accounts.rs