结构中枚举的生命周期参数

时间:2015-06-11 16:04:54

标签: struct enums rust lifetime

我不明白为什么我会遇到这种结构的错误

    public function makeinvoicesAction()
{
    $request = $this->getRequest();

    // Make sure that we are running in a console and the user has not tricked our
    // application into running this action from a public web server.
    if (!$request instanceof ConsoleRequest){
        throw new \RuntimeException('You can only use this action from a console!');
    }
    $verbose = $request->getParam('verbose') || $request->getParam('v');
    if ($verbose) echo "Iniciado el proceso de facturacion";

    $today = new \DateTime();

    $cInvoices = 0;
    $months    = 0;

    $dql    = "SELECT i.number FROM Billing\Entity\Invoice i ORDER BY i.number DESC";
    $query  = $this->getEntityManager()->createQuery($dql);
    $query->setMaxResults(1);
    $result = $query->getArrayResult();
    $numInvoice = intval($result[0]['number']) + 1;

    $dql       = sprintf("SELECT c FROM Customer\Entity\Contract c WHERE c.dueDate = '%s' AND c.status = '%d' AND c.enableInvoicing = true", $today->format('Y-m-d'), \Customer\Entity\Contract::STATUS_ACTIVE);
    $query     = $this->getEntityManager()->createQuery($dql);
    $contracts = $query->iterate();

    foreach ($contracts as $row) {
        $contract    = $row[0];
        $tax         = $this->getEntityManager()->find('Setup\Entity\Tax', 1);
        $invoice     = new \Billing\Entity\Invoice();
        $invoiceItem = new \Billing\Entity\InvoiceItem();

        //Calcs for item
        $invoiceItem->type = \Billing\Entity\InvoiceItem::TYPE_SERVICE;
        $invoiceItem->description = $contract->product->name;
        $invoiceItem->quantity = 1;
        switch ($contract->invoiceFrecuency) {
            case \Customer\Entity\Contract::FRECUENCY_MONTHLY:
                $invoiceItem->unitPrice = $contract->product->priceMonthly;
                $months = 1;
                break;
            case \Customer\Entity\Contract::FRECUENCY_QUARTERLY:
                $invoiceItem->unitPrice = $contract->product->priceQuarterly;
                $months = 3;
                break;
            case \Customer\Entity\Contract::FRECUENCY_ANNUAL:
                $invoiceItem->unitPrice = $contract->product->priceAnnual;
                $months = 12;
                break;
            case \Customer\Entity\Contract::FRECUENCY_BIANNUAL:
                $invoiceItem->unitPrice = $contract->product->priceBiannual;
                $months = 24;
                break;
            default:
                continue;
                break;
        }
        $invoiceItem->amount = ($invoiceItem->quantity * $invoiceItem->unitPrice) / (1 + $tax->rate);
        $invoiceItem->taxAmount = $invoiceItem->amount * $tax->rate;
        $invoiceItem->tax = $tax;
        $invoiceItem->product = $contract->product;
        $invoiceItem->invoice = $invoice;
        $invoiceItem->tsCreated = new \DateTime('now');

        //Creation of new invoice
        $today = new \DateTime('now');
        $invoice->date = new \DateTime('now');
        $invoice->dueDate = $today->add(new \DateInterval('P'. $contract->product->dueDayFirst .'D'));
        $invoice->number  = $numInvoice;
        $invoice->credit = 0;
        $invoice->status = \Billing\Entity\Invoice::STATUS_UNPAID;
        $invoice->addItem($invoiceItem);
        $invoice->contract = $contract;
        $invoice->tsCreated = new \DateTime('now');

        //Update contract
        $contract->dueDate->add(new \DateInterval('P' . $months . 'M'));

        $this->getEntityManager()->persist($invoice);
        $this->getEntityManager()->persist($invoiceItem);
        if (($cInvoices % self::BATCH_SIZE) === 0) {
            $this->getEntityManager()->flush();
            $this->getEntityManager()->clear();
        }
        ++$cInvoices;
        ++$numInvoice;
    }
    $this->getEntityManager()->flush();
    $this->getservicelocator()->get('Logger')->info('Creadas ' . $cInvoices . ' facturas');
    if ($verbose) echo "\nCreadas $cInvoices facturas\nFinalizado el proceso\n";
}

我收到错误

enum Cell <'a> {
    Str(&'a str),
    Double(&'a f32),
}

struct MyCellRep<'a> {
    value: &'a Cell,
    ptr: *const u8,
}

impl MyCellRep{
    fn new_from_str(s: &str) {
        MyCellRep { value: Cell::Str(&s), ptr: new_sCell(CString::new(&s)) }
    }

    fn new_from_double(d: &f32) {
        MyCellRep { value: Cell::Double(&d), ptr: new_dCell(&d) }
    }
}

所以我也尝试了

14:22 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
src\lib.rs:14     value : & 'a Cell ,

但得到了

struct MyCellRep<'a> {
    value: &'a Cell + 'a,
    ptr: *const u8,
}

我认为14:22 error: expected a path on the left-hand side of `+`, not `&'a Cell` 的生命周期应为Cell,而MyCellRepCell::Str至少应具有Cell::Double的生命周期。

最终我能做的就是说

Cell

更新 我想补充一点,通过更改Cell定义,其他代码也应该更改为以下内容供其他人搜索答案。

let x = MyCellRef::new_from_str("foo");
let y = MyCellRef::new_from_double(123.0);

我对Rust的喜爱就像OCaml一样,如果它编译它的作用:)

2 个答案:

答案 0 :(得分:4)

您(可以理解)误解了错误消息:

14:22 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
src\lib.rs:14     value : & 'a Cell ,

你想&#34;但我提供了终身参数!它是'a!&#34;但是,编译器试图告诉您,您没有为 Cell提供生命周期参数(而不是对它的引用):

Cell<'a>

答案 1 :(得分:3)

mdup is correct,但错误消息可以帮助您。出于某种原因,许多人忽略指向错误的错误消息部分:

<anon>:7:16: 7:20 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
<anon>:7     value: &'a Cell,
                        ^~~~

有时,我想提交一个PR,使^~~~~在终端中闪烁^ _ ^。